package com.hamster.crmpre.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hamster.crmpre.config.LoginInterceptor;
import com.hamster.crmpre.exception.ExceptionEnum;
import com.hamster.crmpre.exception.LyException;
import com.hamster.crmpre.mapper.*;
import com.hamster.crmpre.pojo.*;
import com.hamster.crmpre.service.PhoneUserService;
import com.hamster.crmpre.service.PotentialCustomerService;
import com.hamster.crmpre.service.ProjectBuyService;
import com.hamster.crmpre.util.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Tarry
 * @create 2019/12/24 20:30
 */
@Service
public class ProjectBuyServiceImpl implements ProjectBuyService {
    @Autowired
    private CustomerRecordMapper customerRecordMapper; //购买记录mapper
    @Autowired
    private CustomerMapper customerMapper;  //客户mapper
    @Autowired
    private MessageBuyVerifyMapper messageBuyVerifyMapper; //开发客户审核消息mapper
    @Autowired
    private PersonalMessageMapper personalMessageMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private CompanyAccountMapper companyAccountMapper;
    @Autowired
    private CustomerCourseClassMapper customerCourseClassMapper;
    @Autowired
    private CustomerCourseClassTimeMapper customerCourseClassTimeMapper;
    @Autowired
    private ProjectCourseMapper projectCourseMapper;
    @Autowired
    private CourseTimeMapper courseTimeMapper;
    @Autowired
    private EmployeePerformanceMapper employeePerformanceMapper;
    @Autowired
    private HandOverVipMapper handOverVipMapper;//转交客户  vip  小分公司  大分公司
    @Autowired
    private ExecutivePerformanceMapper executivePerformanceMapper;
    @Autowired
    private PotentialCustomerService potentialCustomerService;
    @Autowired
    private ManagementProfitConfigMapper managementProfitConfigMapper;
    @Autowired
    private ManagerPerformanceMapper managerPerformanceMapper;
    @Autowired
    private DirectorPerformanceMapper directorPerformanceMapper;
    @Autowired
    private CustomerVipMapper customerVipMapper;
    @Autowired
    private BranchProfitConfigMapper branchProfitConfigMapper;
    @Autowired
    private CustomerSmallCompanyMapper customerSmallCompanyMapper;
    @Autowired
    private CustomerBigCompanyMapper customerBigCompanyMapper;
    @Autowired
    private SmallCompanyRecordMapper smallCompanyRecordMapper;
    @Autowired
    private BigCompanyRecordMapper bigCompanyRecordMapper;
    @Autowired
    private PersonalMoneyMapper personalMoneyMapper;
    @Autowired
    private CustomerVipRecordMapper customerVipRecordMapper;
    @Autowired
    private PartnerProfitConfigMapper partnerProfitConfigMapper;
    @Autowired
    private CareerProfitConfigMapper careerProfitConfigMapper;
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private PhoneUserService phoneUserService;
    /**
     * 购买项目
     * @param customerRecord
     */
    @Transactional
    @Override
    public void insert(CustomerRecord customerRecord) {
        Date date = new Date();
        //获取登录用户的信息
        UserInfo loginUser = LoginInterceptor.getLoginUser();


        //判断是 分公司开发  还是 员工开发
        if ("客户".equals(loginUser.getRole())){//分公司开发

            customerRecord.setId(null);//插入数据前置空
            customerRecord.setInputTime(date);
            String userName = phoneUserService.queryNameByPhone(LoginInterceptor.getLoginUser().getPhone());
            customerRecord.setInputUser(userName);
            customerRecord.setDel("0");         //记录状态
            customerRecord.setOrderState("0");  //订单状态  0待审核  1合格   2 不合格
            customerRecord.setRegdate(date);    //报名购买时间
            customerRecord.setDeveloper(userName);//开发者姓名
            customerRecord.setDeveloperId(loginUser.getUserId());//开发者id
            customerRecord.setBranchCompanyId(loginUser.getUserId());//分公司的id
            customerRecord.setType("1"); //0 员工开发    1 分公司开发


        }else if ("员工".equals(loginUser.getRole())){//员工开发
            customerRecord.setId(null);//插入数据前置空
            customerRecord.setInputTime(date);
            String userName = phoneUserService.queryNameByPhone(LoginInterceptor.getLoginUser().getPhone());
            customerRecord.setInputUser(userName);
            customerRecord.setDel("0");         //记录状态
            customerRecord.setOrderState("0");  //订单状态  0待审核  1合格   2 不合格
            customerRecord.setRegdate(date);    //报名购买时间
           // String userName = phoneUserService.queryNameByPhone(LoginInterceptor.getLoginUser().getPhone());
            customerRecord.setDeveloper(userName);//开发者姓名loginUser.getName()
            customerRecord.setDeveloperId(loginUser.getUserId());//开发者id
            customerRecord.setType("0");
        }

        //1.像购买记录表插入记录
        customerRecordMapper.insert(customerRecord);

        //2.购买项目消息表插入一条记录,并在  个人消息表，中添加记录通知财务
        this.insertBuyMessage(customerRecord);

        //财务审核通过之后，进行其他操作，代码在其他位置


    }

    @Transactional
    //添加购买消息记录，通知会计审核
    public void insertBuyMessage(CustomerRecord customerRecord) {
        String userName = phoneUserService.queryNameByPhone(LoginInterceptor.getLoginUser().getPhone());
      //  customerRecord.setDeveloper(LoginInterceptor.getLoginUser().getName());//开发者姓名
        customerRecord.setDeveloper(userName);//开发者姓名
        customerRecord.setDeveloperId(LoginInterceptor.getLoginUser().getUserId());//开发者id
        MessageBuyVerify messageBuyVerify = new MessageBuyVerify();
        messageBuyVerify.setCustomerRecordId(customerRecord.getId());//设置id
        messageBuyVerify.setContent(JsonUtils.serialize(customerRecord));//设置内容
        messageBuyVerify.setState("0");
        if ("0".equals(customerRecord.getType())) {//员工开发
            //根据项目设置好的提成进行计算。
            messageBuyVerify.setMoney(customerRecord.getMoney() * customerRecord.getProfit());
            //存入具体的开发客户审核消息表
            messageBuyVerifyMapper.insert(messageBuyVerify);
            String verifyId = messageBuyVerify.getId();  //获取审核消息详情的id

            //在个人消息表中插入该记录
            PersonalMessage personalMessage = new PersonalMessage();
            Date date = new Date();
            personalMessage.setDel("0");
            personalMessage.setInputTime(date);
            personalMessage.setMessageType("开发客户");
            personalMessage.setMessageLink(verifyId);//存入对应类型消息表的id
            personalMessage.setCreateTime(date);
            personalMessage.setState("0");
            //消息发送人，接收人
            personalMessage.setSenderId(customerRecord.getDeveloperId());//设置发送者id
            personalMessage.setSenderName(customerRecord.getDeveloper());//设置发送者姓名
            personalMessage.setReceiverName("财务部");
            personalMessage.setReceiverId("财务部");
            personalMessage.setDepartment("财务部");
            personalMessageMapper.insert(personalMessage);
        } else if ("1".equals(customerRecord.getType())) { //分公司开发
            // 看开发的这个项目是什么，生成对应的提成
            //如果 分公司还有剩余课程款，没有提成。不属于分公司课程款，则计算对应提成

            //存入具体的开发客户审核消息表
            messageBuyVerifyMapper.insert(messageBuyVerify);
            String verifyId = messageBuyVerify.getId();  //获取审核消息详情的id
            //在个人消息表中插入该记录
            PersonalMessage personalMessage = new PersonalMessage();
            Date date = new Date();
            personalMessage.setDel("0");
            personalMessage.setInputTime(date);
            personalMessage.setMessageType("分公司开发客户");
            personalMessage.setMessageLink(verifyId);//存入对应类型消息表的id
            personalMessage.setCreateTime(date);
            personalMessage.setState("0");
            //消息发送人，接收人
            personalMessage.setSenderId(customerRecord.getDeveloperId());//设置发送者id
            personalMessage.setSenderName(customerRecord.getDeveloper());//设置发送者姓名
            personalMessage.setReceiverName("财务部");
            personalMessage.setReceiverId("财务部");
            personalMessage.setDepartment("财务部");
            personalMessageMapper.insert(personalMessage);
        }
    }
    /**
     * 根据手机号查询推荐人
     * @param phone
     * @return
     */
    @Override
    public Customer queryCustomerByPhone(String phone) {
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        Customer customer = customerMapper.selectOne(queryWrapper);
        return customer;
    }

    /**
     * 开发客户审批回调之后，调用该方法进行后续操作
     * @param messageId  消息中心的id
     */
    @Transactional
    @Override
    public void buyProjectPassed(String messageId){
        // 主管。总监。经理的 提成比例
        ManagementProfitConfig managementProfitConfig = managementProfitConfigMapper.selectById("1");
        //分公司返利设置
        BranchProfitConfig branchProfitConfig = branchProfitConfigMapper.selectById("1");
        //合伙人返利设置
        PartnerProfitConfig partnerProfitConfig = partnerProfitConfigMapper.selectById("1");

        //查询出，购买记录中的详细信息
        PersonalMessage personalMessage = personalMessageMapper.selectById(messageId);//查消息中心
        String verifyId = personalMessage.getMessageLink();//购买项目审核消息表的id
        MessageBuyVerify messageBuyVerify = messageBuyVerifyMapper.selectById(verifyId);//详细的审核通知内容对象
        String customerRecordId = messageBuyVerify.getCustomerRecordId();//购买记录表的id
        CustomerRecord customerRecord = customerRecordMapper.selectById(customerRecordId);//购买记录对象
        Project project = projectMapper.selectById(customerRecord.getProjectId());//购买的项目
        String categoryId = projectMapper.queryDepartmentById(customerRecord.getProjectId());//项目的分类


        if ("0".equals(customerRecord.getType()))//员工开发
        {
            //1.给客户表添加购买的项目

            Double money = messageBuyVerify.getMoney();//业绩的金额
            //先判断该客户是不是第一次购买课程
            QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("phone", customerRecord.getPhone());
            Customer customer = customerMapper.selectOne(queryWrapper);
            if (customer == null) {//首次购买【将购买记录的数据存入客户表】
                Customer firstCustomer = new Customer();//这里修改过 ，将firstCustomer 修改成 customer
                customer = new Customer();
                List<String> pTitle = new ArrayList<>();
                pTitle.add(customerRecord.getProjectTitle());
                List<String> pId = new ArrayList<>();
                pId.add(customerRecord.getProjectId());
                String titleList = JsonUtils.serialize(pTitle);
                String idList = JsonUtils.serialize(pId);

                customer.setProjectId(idList);//已购买项目id数组
                customer.setProjectTitle(titleList);//已购买项目名称数组
                customer.setName(customerRecord.getName());//设置客户姓名
                customer.setWxNumber(customerRecord.getWxNumber());//设置微信号
                customer.setPhone(customerRecord.getPhone());   //设置微信号
                customer.setGender(customerRecord.getGender());  //设置性别
                customer.setPosition(customerRecord.getPosition());//职位
                customer.setRegion(customerRecord.getRegion()); //地区
                customer.setDeveloperId(customerRecord.getDeveloperId());//开发员工id
                customer.setType("0"); //0 员工开发    1分公司开发


                //查询开发者的部门
                QueryWrapper<Employee> employeeQueryWrapper = new QueryWrapper<>();
                employeeQueryWrapper.eq("id", customerRecord.getDeveloperId());
                String department = employeeMapper.selectOne(employeeQueryWrapper).getDepartment();//开发者的部门
                customer.setDepartment(department);//设置部门为，开发者的部门，仅方便总监查询
                customer.setPayment(customerRecord.getPayment());//设置是否全款
                customer.setChildrenAge(customerRecord.getChildrenAge());
                customer.setOrganization(customerRecord.getOrganization());
                customer.setBusinessProject(customerRecord.getBusinessProject());
                customer.setTeacherNumber(customerRecord.getTeacherNumber());
                customer.setInstitutionalProblem(customerRecord.getInstitutionalProblem());
                customer.setHaveLessons(customerRecord.getHaveLessons());
                customer.setIdNumber(customerRecord.getIdNumber());
                customer.setBranchAttribute(customerRecord.getBranchAttribute());
                customer.setBalance(customerRecord.getBalance());//设置首付交了多少钱
                customer.setBankNumber(customerRecord.getBankNumber());
                customer.setAlipay(customerRecord.getAlipay());
                //                设置好信息之后，插入到表中
                customerMapper.insert(customer);
                //根据用户购买的项目分类，和开发员工所属部门，进行判断负责人该如何分配
                //查询客户购买项目的分类
                //获取该项目所属的分类id 1课程  2vip  3小分公司  4大分公司
                //TODO 课程级别的负责人是开发者，vip和分公司需要交给指定部门

                switch (categoryId) {
                    case "1"://课程级别
                        customer.setPrincipalCourseId(customerRecord.getDeveloperId());//课程级别负责人id
                        //TODO 后面代码已添加课程

                        break;
                    case "2"://VIP 级别
                        //给vip详情表加入一条记录
                        QueryWrapper<CustomerVip> customerVipQueryWrapper = new QueryWrapper<>();
                        customerVipQueryWrapper.eq("customer_id", customer.getId());
                        CustomerVip customerVip = customerVipMapper.selectOne(customerVipQueryWrapper);
                        //1.判断有没有，如果没有则新增
                        if (customerVip == null) {
                            customerVip = new CustomerVip();
                            customerVip.setState("0"); //0 vip  1 合伙人
                            customerVip.setCount(0);//推荐的次数
                            customerVip.setCustomerName(customer.getName());//客户姓名
                            customerVip.setCustomerId(customer.getId());//客户id
                            customerVipMapper.insert(customerVip);//在vip详细表中增加一条记录
                        }
                        //如果开发者是服务部 则，vip负责人是自己，否则交给服务部经理分配
                        //1开发部 2服务部  3事业部 4财务部  5后勤部 6人事部
                        if (department.equals("2")) {
                            customer.setPrincipalVipId(customerRecord.getDeveloperId());
                        } else {
                            //TODO 给服务部总监发消息  待完成************分配客户给员工*************
                            //TODO 后面代码已添加课程
                            //新建转交客户的消息【这里仅仅是名字有vip，功能可以转 分公司】
                            HandOverVip handOverVip = new HandOverVip();
                            handOverVip.setDel("0");
                            handOverVip.setState("0");
                            handOverVip.setEmployeeId(customerRecord.getDeveloperId());
                            handOverVip.setEmployeeName(customerRecord.getDeveloper());
                            handOverVip.setProjectId(customerRecord.getProjectId());//分配时用来区分，修改哪个负责人字段
                            handOverVip.setVipCustomerId(customer.getId());//购买客户的id
                            handOverVip.setVipCustomerName(customer.getName());//购买客户的姓名
                            handOverVipMapper.insert(handOverVip);//插入消息详情
                            //新建消息中心的消息
                            PersonalMessage personalMessage1 = new PersonalMessage();
                            personalMessage1.setDepartment("服务部");
                            personalMessage1.setSenderId(customerRecord.getDeveloperId());//发送者id
                            personalMessage1.setSenderName(customerRecord.getDeveloper());//发送者姓名
                            personalMessage1.setMessageType("转交VIP");//消息类型
                            personalMessage1.setState("0");//0 未读  1 已读
                            personalMessage1.setMessageLink(handOverVip.getId());
                            personalMessage1.setReceiverName("服务部");
                            personalMessage1.setReceiverId("服务部");
                            personalMessage1.setCreateTime(new Date());
                            personalMessageMapper.insert(personalMessage1);
                        }


                        break;
                    case "3"://小分公司级别


                        //如果开发者是事业部 则，分公司是自己，否则交给事业部经理分配
                        //1开发部 2服务部  3事业部 4财务部  5后勤部 6人事部
                        if (department.equals("3")) {
                            customer.setPrincipalCompanyId(customerRecord.getDeveloperId());
                        } else {
                            //TODO 给事业部总监发消息  待完成*************分配客户给员工************
                            //新建转交客户的消息【这里仅仅是名字有vip，功能可以转 分公司】
                            HandOverVip handOverVip = new HandOverVip();
                            handOverVip.setDel("0");
                            handOverVip.setState("0");
                            handOverVip.setEmployeeId(customerRecord.getDeveloperId());
                            handOverVip.setEmployeeName(customerRecord.getDeveloper());
                            handOverVip.setProjectId(customerRecord.getProjectId());//分配时用来区分，修改哪个负责人字段
                            handOverVip.setVipCustomerId(customer.getId());//购买客户的id
                            handOverVip.setVipCustomerName(customer.getName());//购买客户的姓名
                            handOverVipMapper.insert(handOverVip);//插入消息详情
                            //新建消息中心的消息
                            PersonalMessage personalMessage1 = new PersonalMessage();
                            personalMessage1.setDepartment("事业部");
                            personalMessage1.setSenderId(customerRecord.getDeveloperId());//发送者id
                            personalMessage1.setSenderName(customerRecord.getDeveloper());//发送者姓名
                            personalMessage1.setMessageType("转交小分公司");//消息类型
                            personalMessage1.setState("0");//0 未读  1 已读
                            personalMessage1.setMessageLink(handOverVip.getId());
                            personalMessage1.setReceiverName("事业部");
                            personalMessage1.setReceiverId("事业部");
                            personalMessage1.setCreateTime(new Date());
                            personalMessageMapper.insert(personalMessage1);
                        }

                        break;
                    case "4"://大分公司级别


                        //如果开发者是事业部 则，分公司是自己，否则交给事业部经理分配
                        //1开发部 2服务部  3事业部 4财务部  5后勤部 6人事部
                        if (department.equals("3")) {
                            customer.setPrincipalCompanyId(customerRecord.getDeveloperId());
                        } else {
                            //TODO 给事业部总监发消息  待完成*************分配客户给员工************
                            //新建转交客户的消息【这里仅仅是名字有vip，功能可以转 分公司】
                            HandOverVip handOverVip = new HandOverVip();
                            handOverVip.setDel("0");
                            handOverVip.setState("0");
                            handOverVip.setEmployeeId(customerRecord.getDeveloperId());
                            handOverVip.setEmployeeName(customerRecord.getDeveloper());
                            handOverVip.setProjectId(customerRecord.getProjectId());//分配时用来区分，修改哪个负责人字段
                            handOverVip.setVipCustomerId(customer.getId());//购买客户的id
                            handOverVip.setVipCustomerName(customer.getName());//购买客户的姓名
                            handOverVipMapper.insert(handOverVip);//插入消息详情
                            //新建消息中心的消息
                            PersonalMessage personalMessage1 = new PersonalMessage();
                            personalMessage1.setDepartment("事业部");
                            personalMessage1.setSenderId(customerRecord.getDeveloperId());//发送者id

                            personalMessage1.setSenderName(customerRecord.getDeveloper());//发送者姓名
                            personalMessage1.setMessageType("转交大分公司");//消息类型
                            personalMessage1.setState("0");//0 未读  1 已读
                            personalMessage1.setMessageLink(handOverVip.getId());
                            personalMessage1.setReceiverName("事业部");
                            personalMessage1.setReceiverId("事业部");
                            personalMessage1.setCreateTime(new Date());
                            personalMessageMapper.insert(personalMessage1);
                        }
                        break;
                }
                //更新客户个人资料
                customerMapper.updateById(customer);

            } else {//多次购买
                //获取原来购买的项目，然后把新购买的项目添加进去
                List<String> newProjectId = JsonUtils.parseList(customer.getProjectId(), String.class);
                List<String> newProjectTitle = JsonUtils.parseList(customer.getProjectTitle(), String.class);
                newProjectId.add(customerRecord.getProjectId());
                newProjectTitle.add(customerRecord.getProjectTitle());
                String idList = JsonUtils.serialize(newProjectId);
                String titleList = JsonUtils.serialize(newProjectTitle);
                //    设置要更新修改的信息字段
                customer.setProjectId(idList);
                customer.setProjectTitle(titleList);
                customer.setPayment(customerRecord.getPayment());//设置是否全款
                customer.setChildrenAge(customerRecord.getChildrenAge());
                customer.setOrganization(customerRecord.getOrganization());
                customer.setBusinessProject(customerRecord.getBusinessProject());
                customer.setTeacherNumber(customerRecord.getTeacherNumber());
                customer.setInstitutionalProblem(customerRecord.getInstitutionalProblem());
                customer.setHaveLessons(customerRecord.getHaveLessons());
                customer.setIdNumber(customerRecord.getIdNumber());
                customer.setBranchAttribute(customerRecord.getBranchAttribute());
                customer.setBalance(customerRecord.getBalance());
                customer.setBankNumber(customerRecord.getBankNumber());
                customer.setAlipay(customerRecord.getAlipay());

                //查询开发者的部门
                QueryWrapper<Employee> employeeQueryWrapper = new QueryWrapper<>();
                employeeQueryWrapper.eq("id", customerRecord.getDeveloperId());
                String department = employeeMapper.selectOne(employeeQueryWrapper).getDepartment();//开发者的部门

                //根据用户购买的项目分类，和开发员工所属部门，进行判断负责人该如何分配
                //查询客户购买项目的分类
                //获取该项目所属的分类id 1课程  2vip  3小分公司  4大分公司
                //TODO 课程级别的负责人是开发者，vip和分公司需要交给指定部门

                switch (categoryId) {
                    case "1":
                        customer.setPrincipalCourseId(customerRecord.getDeveloperId());
                        //TODO 后面代码已添加课程
                        break;
                    case "2":
                        //给vip详情表加入一条记录
                        QueryWrapper<CustomerVip> customerVipQueryWrapper = new QueryWrapper<>();
                        customerVipQueryWrapper.eq("customer_id", customer.getId());
                        CustomerVip customerVip = customerVipMapper.selectOne(customerVipQueryWrapper);
                        //1.判断有没有，如果没有则新增
                        if (customerVip == null) {
                            customerVip = new CustomerVip();
                            customerVip.setState("0"); //0 vip  1 合伙人
                            customerVip.setCount(0);//推荐的次数
                            customerVip.setCustomerName(customer.getName());//客户姓名
                            customerVip.setCustomerId(customer.getId());//客户id
                            customerVipMapper.insert(customerVip);//在vip详细表中增加一条记录
                        }


                        //如果开发者是服务部 则，vip负责人是自己，否则交给服务部经理分配
                        //1开发部 2服务部  3事业部 4财务部  5后勤部 6人事部
                        if (department.equals("2")) {
                            customer.setPrincipalVipId(customerRecord.getDeveloperId());
                        } else {
                            //TODO 给服务部总监发消息  待完成************分配客户给员工*************
                            //TODO 后面代码已添加课程
                            //新建转交客户的消息【这里仅仅是名字有vip，功能可以转 分公司】
                            HandOverVip handOverVip = new HandOverVip();
                            handOverVip.setDel("0");
                            handOverVip.setState("0");
                            handOverVip.setEmployeeId(customerRecord.getDeveloperId());
                            handOverVip.setEmployeeName(customerRecord.getDeveloper());
                            handOverVip.setProjectId(customerRecord.getProjectId());//分配时用来区分，修改哪个负责人字段
                            handOverVip.setVipCustomerId(customer.getId());//购买客户的id
                            handOverVip.setVipCustomerName(customer.getName());//购买客户的姓名
                            handOverVipMapper.insert(handOverVip);//插入消息详情
                            //新建消息中心的消息
                            PersonalMessage personalMessage1 = new PersonalMessage();
                            personalMessage1.setDepartment("服务部");
                            personalMessage1.setSenderId(customerRecord.getDeveloperId());//发送者id
                            personalMessage1.setSenderName(customerRecord.getDeveloper());//发送者姓名
                            personalMessage1.setMessageType("转交VIP");//消息类型
                            personalMessage1.setState("0");//0 未读  1 已读
                            personalMessage1.setMessageLink(handOverVip.getId());
                            personalMessage1.setReceiverName("服务部");
                            personalMessage1.setReceiverId("服务部");
                            personalMessage1.setCreateTime(new Date());
                            personalMessageMapper.insert(personalMessage1);

                        }
                        break;
                    case "3":


                        //如果开发者是事业部 则，分公司是自己，否则交给事业部经理分配
                        //1开发部 2服务部  3事业部 4财务部  5后勤部 6人事部
                        if (department.equals("3")) {
                            customer.setPrincipalCompanyId(customerRecord.getDeveloperId());
                        } else {
                            //TODO 给事业部总监发消息  待完成*************分配客户给员工************
                            //新建转交客户的消息【这里仅仅是名字有vip，功能可以转 分公司】
                            HandOverVip handOverVip = new HandOverVip();
                            handOverVip.setDel("0");
                            handOverVip.setState("0");
                            handOverVip.setEmployeeId(customerRecord.getDeveloperId());
                            handOverVip.setEmployeeName(customerRecord.getDeveloper());
                            handOverVip.setProjectId(customerRecord.getProjectId());//分配时用来区分，修改哪个负责人字段
                            handOverVip.setVipCustomerId(customer.getId());//购买客户的id
                            handOverVip.setVipCustomerName(customer.getName());//购买客户的姓名
                            handOverVipMapper.insert(handOverVip);//插入消息详情
                            //新建消息中心的消息
                            PersonalMessage personalMessage1 = new PersonalMessage();
                            personalMessage1.setDepartment("事业部");
                            personalMessage1.setSenderId(customerRecord.getDeveloperId());//发送者id
                            personalMessage1.setSenderName(customerRecord.getDeveloper());//发送者姓名
                            personalMessage1.setMessageType("转交小分公司");//消息类型
                            personalMessage1.setState("0");//0 未读  1 已读
                            personalMessage1.setMessageLink(handOverVip.getId());
                            personalMessage1.setReceiverName("事业部");
                            personalMessage1.setReceiverId("事业部");
                            personalMessage1.setCreateTime(new Date());
                            personalMessageMapper.insert(personalMessage1);

                        }

                        break;
                    case "4":


                        //如果开发者是事业部 则，分公司是自己，否则交给事业部经理分配
                        //1开发部 2服务部  3事业部 4财务部  5后勤部 6人事部
                        if (department.equals("3")) {
                            customer.setPrincipalCompanyId(customerRecord.getDeveloperId());
                        } else {
                            //TODO 给事业部总监发消息  待完成*************分配客户给员工************

                            //新建转交客户的消息【这里仅仅是名字有vip，功能可以转 分公司】
                            HandOverVip handOverVip = new HandOverVip();
                            handOverVip.setDel("0");
                            handOverVip.setState("0");
                            handOverVip.setEmployeeId(customerRecord.getDeveloperId());
                            handOverVip.setEmployeeName(customerRecord.getDeveloper());
                            handOverVip.setProjectId(customerRecord.getProjectId());//分配时用来区分，修改哪个负责人字段
                            handOverVip.setVipCustomerId(customer.getId());//购买客户的id
                            handOverVip.setVipCustomerName(customer.getName());//购买客户的姓名
                            handOverVipMapper.insert(handOverVip);//插入消息详情
                            //新建消息中心的消息
                            PersonalMessage personalMessage1 = new PersonalMessage();
                            personalMessage1.setDepartment("事业部");
                            personalMessage1.setSenderId(customerRecord.getDeveloperId());//发送者id
                            personalMessage1.setSenderName(customerRecord.getDeveloper());//发送者姓名
                            personalMessage1.setMessageType("转交大分公司");//消息类型
                            personalMessage1.setState("0");//0 未读  1 已读
                            personalMessage1.setMessageLink(handOverVip.getId());
                            personalMessage1.setReceiverName("事业部");
                            personalMessage1.setReceiverId("事业部");
                            personalMessage1.setCreateTime(new Date());
                            personalMessageMapper.insert(personalMessage1);

                        }
                        break;
                }
                //更新客户个人资料
                customerMapper.updateById(customer);
            }


            //4.给员工业绩表添加员工业绩
            Employee employee = employeeMapper.selectById(customerRecord.getDeveloperId());//员工对象
            EmployeePerformance employeePerformance = new EmployeePerformance();
            employeePerformance.setEmployeeProfit(money);//设置员工业绩提成
            employeePerformance.setEmployeeId(customerRecord.getDeveloperId());//员工id
            employeePerformance.setEmployeeName(customerRecord.getDeveloper());//员工姓名
            employeePerformance.setCustomer(customer.getName());//客户姓名
            employeePerformance.setCustomerId(customer.getId());//客户id
            employeePerformance.setProjectId(customerRecord.getProjectId());//购买项目id
            employeePerformance.setPurchaseItem(customerRecord.getProjectTitle());//购买项目名称
            employeePerformance.setInputTime(new Date()); //业绩的录入日期
            employeePerformance.setType("0");  //0购买产品    1项目加盟    2
            //TODO 根据后台管理的员工上级主管的id
            employeePerformance.setExecutiveId(employee.getSuperiorId());//上级管理的id
            employeePerformanceMapper.insert(employeePerformance); //新增一条员工业绩

            ////总账减去员工提成
            //给公司总账，添加一条明细记录
            CompanyAccount companyAccount0 = new CompanyAccount();
            companyAccount0.setTitle("开发客户员工分成");            //公司明细标题
            companyAccount0.setCreateTime(new Date());       //创建时间
            companyAccount0.setMoney(-money);//支出金额
            companyAccount0.setContent("员工："+employee.getName()+","+employee.getPhone()+" 分得提成 "+ money);//记账内容
            companyAccountMapper.insert(companyAccount0);

            //存入 个人流水账 表
            PersonalMoney personalMoneyEmp0 = new PersonalMoney();
            personalMoneyEmp0.setTitle("开发客户");
            personalMoneyEmp0.setCreateTime(new Date());
            personalMoneyEmp0.setContent("开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle());
            personalMoneyEmp0.setType("1");//0 客户[分公司、合伙人]   1 员工
            personalMoneyEmp0.setPersonId(customerRecord.getDeveloperId());//个人id【流水账主人】
            personalMoneyEmp0.setMoney(money);//金额
            personalMoneyMapper.insert(personalMoneyEmp0);

            //如果该开发员工是【开发部员工】则进行如下步骤，如果是主管或经理不管了，需求没提到
            if (employee.getPosition().equals("0") && employee.getDepartment().equals("1")){
                //获取主管的信息
                Employee execuitiveEmployee = employeeMapper.selectById(employee.getSuperiorId());
                //插入主管的业绩
                ExecutivePerformance executivePerformance = new ExecutivePerformance();
                executivePerformance.setCustomer(customer.getName());//客户姓名
                executivePerformance.setCustomerId(customer.getId());//客户id
                executivePerformance.setEmployeeId(execuitiveEmployee.getId());//主管id
                executivePerformance.setEmployeeName(execuitiveEmployee.getName());//主管姓名
                executivePerformance.setPurchaseItem(customerRecord.getProjectTitle());//项目名称
                executivePerformance.setProjectId(customerRecord.getProjectId());//项目id
                executivePerformance.setManagerId(execuitiveEmployee.getSuperiorId());//上级经理id
                executivePerformance.setEmployeeProfit(money);//员工提成
                Double executiveProfit = money * managementProfitConfig.getDevelopmentLead();
                executivePerformance.setExecutiveProfit(executiveProfit);//开发部主管提成
                executivePerformance.setDeveloperId(customerRecord.getDeveloperId());//开发员工id
                executivePerformance.setDeveloperName(customerRecord.getDeveloperId());//开发员工姓名

                executivePerformanceMapper.insert(executivePerformance);
                //给公司总账，添加一条明细记录
                CompanyAccount companyAccount1 = new CompanyAccount();
                companyAccount1.setTitle("开发客户主管分成");            //公司明细标题
                companyAccount1.setCreateTime(new Date());       //创建时间
                companyAccount1.setMoney(-executiveProfit);//支出金额
                companyAccount1.setContent("主管："+execuitiveEmployee.getName()+","+execuitiveEmployee.getPhone()+" 分得提成 "+ executiveProfit);//记账内容
                companyAccountMapper.insert(companyAccount1);

                //存入【主管】 个人流水账 表
                PersonalMoney personalMoneyEmp1 = new PersonalMoney();
                personalMoneyEmp1.setTitle("员工开发客户");
                personalMoneyEmp1.setCreateTime(new Date());
                personalMoneyEmp1.setContent(customerRecord.getDeveloper()+"开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle());
                personalMoneyEmp1.setType("1");//0 客户[分公司、合伙人]   1 员工
                personalMoneyEmp1.setPersonId(execuitiveEmployee.getId());//个人id【流水账主人】
                personalMoneyEmp1.setMoney(executiveProfit);//金额
                personalMoneyMapper.insert(personalMoneyEmp1);

                //插入经理的业绩
                Double manageProfit = executiveProfit * managementProfitConfig.getDevelopmentManager();
                Employee managerEmployee = employeeMapper.selectById(execuitiveEmployee.getSuperiorId());
                ManagerPerformance managerPerformance = new ManagerPerformance();
                managerPerformance.setCustomer(customer.getName());//客户
                managerPerformance.setCustomerId(customer.getId());
                managerPerformance.setDeveloperId(customerRecord.getDeveloperId());//开发者id
                managerPerformance.setDeveloperName(customerRecord.getDeveloper());
                managerPerformance.setEmployeeId(managerEmployee.getId());//经理id
                managerPerformance.setEmployeeName(managerEmployee.getName());
                managerPerformance.setExecutiveProfit(executiveProfit);//主管提成
                managerPerformance.setDirectorId(managerEmployee.getSuperiorId());//上级总监id
                managerPerformance.setManagerProfit(manageProfit);//经理提成
                managerPerformance.setProjectId(customerRecord.getProjectId());//项目id
                managerPerformance.setPurchaseItem(customerRecord.getProjectTitle());

                managerPerformanceMapper.insert(managerPerformance);
                //存入【经理】 个人流水账 表
                PersonalMoney personalMoneyEmp2 = new PersonalMoney();
                personalMoneyEmp2.setTitle("员工开发客户");
                personalMoneyEmp2.setCreateTime(new Date());
                personalMoneyEmp2.setContent(customerRecord.getDeveloper()+"开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle());
                personalMoneyEmp2.setType("1");//0 客户[分公司、合伙人]   1 员工
                personalMoneyEmp2.setPersonId(managerEmployee.getId());//个人id【流水账主人】
                personalMoneyEmp2.setMoney(manageProfit);//金额
                personalMoneyMapper.insert(personalMoneyEmp2);


                //给公司总账，添加一条明细记录
                CompanyAccount companyAccount2 = new CompanyAccount();
                companyAccount2.setTitle("开发客户经理分成");            //公司明细标题
                companyAccount2.setCreateTime(new Date());       //创建时间
                companyAccount2.setMoney(-manageProfit);//支出金额
                companyAccount2.setContent("经理："+managerEmployee.getName()+","+managerEmployee.getPhone()+" 分得提成 "+ manageProfit);//记账内容
                companyAccountMapper.insert(companyAccount2);



                //插入总监业绩
                Double directorProfit = manageProfit * managementProfitConfig.getDevelopmentOfficer();//总监提成
                Employee directorEmployee = employeeMapper.selectById(managerEmployee.getSuperiorId());
                DirectorPerformance directorPerformance = new DirectorPerformance();
                directorPerformance.setCustomer(customer.getName());//客户
                directorPerformance.setCustomerId(customer.getId());
                directorPerformance.setDeveloperId(customerRecord.getDeveloperId());//开发者id
                directorPerformance.setDeveloperName(customerRecord.getDeveloper());
                directorPerformance.setEmployeeId(directorEmployee.getId());//总监id
                directorPerformance.setEmployeeName(directorEmployee.getName());
                directorPerformance.setDirectorProfit(directorProfit);//总监提成
                directorPerformance.setManagerProfit(manageProfit);//经理提成
                directorPerformance.setProjectId(customerRecord.getProjectId());//项目id
                directorPerformance.setPurchaseItem(customerRecord.getProjectTitle());
                directorPerformanceMapper.insert(directorPerformance);
                //给公司总账，添加一条明细记录
                CompanyAccount companyAccount3 = new CompanyAccount();
                companyAccount3.setTitle("开发客户总监分成"); //公司明细标题
                companyAccount3.setCreateTime(new Date()); //创建时间
                companyAccount3.setMoney(-directorProfit);//支出金额
                companyAccount3.setContent("总监："+directorEmployee.getName()+","+directorEmployee.getPhone()+" 分得提成 "+ directorProfit);//记账内容
                companyAccountMapper.insert(companyAccount3);

                //存入【总监】 个人流水账 表
                PersonalMoney personalMoneyEmp3 = new PersonalMoney();
                personalMoneyEmp3.setTitle("员工开发客户");
                personalMoneyEmp3.setCreateTime(new Date());
                personalMoneyEmp3.setContent(customerRecord.getDeveloper()+"开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle());
                personalMoneyEmp3.setType("1");//0 客户[分公司、合伙人]   1 员工
                personalMoneyEmp3.setPersonId(directorEmployee.getId());//个人id【流水账主人】
                personalMoneyEmp3.setMoney(directorProfit);//金额
                personalMoneyMapper.insert(personalMoneyEmp3);
            }

            //如果该开发员工是【服务部或事业部经理】则进行以下步骤
            //如果没有设置上级总监则不进入操作
            Employee directorPanduan = employeeMapper.selectById(employee.getSuperiorId());
            if(StringUtils.isBlank(employee.getSuperiorId()) && directorPanduan != null){
                if ((employee.getPosition().equals("2") && employee.getDepartment().equals("2") )||(employee.getPosition().equals("2") && employee.getDepartment().equals("3"))){

                    //插入总监业绩
                    Double directorProfit = 0.0;//总监提成
                    //判断 是 服务部总监提成   还是   事业部总监提成
                    if (employee.getDepartment().equals("3") ){//事业部
                        directorProfit =  money * managementProfitConfig.getCareerOfficer();
                    }else if (employee.getDepartment().equals("2") ){//服务部
                        directorProfit =  money * managementProfitConfig.getServiceOfficer();
                    }

                    Employee directorEmployee = employeeMapper.selectById(employee.getSuperiorId());
                    DirectorPerformance directorPerformance = new DirectorPerformance();
                    directorPerformance.setCustomer(customer.getName());//客户
                    directorPerformance.setCustomerId(customer.getId());
                    directorPerformance.setDeveloperId(customerRecord.getDeveloperId());//开发者id
                    directorPerformance.setDeveloperName(customerRecord.getDeveloper());
                    directorPerformance.setEmployeeId(directorEmployee.getId());//总监id
                    directorPerformance.setEmployeeName(directorEmployee.getName());
                    directorPerformance.setDirectorProfit(directorProfit);//总监提成
                    directorPerformance.setManagerProfit(money);//经理提成
                    directorPerformance.setProjectId(customerRecord.getProjectId());//项目id
                    directorPerformance.setPurchaseItem(customerRecord.getProjectTitle());
                    directorPerformanceMapper.insert(directorPerformance);
                    //给公司总账，添加一条明细记录
                    CompanyAccount companyAccount3 = new CompanyAccount();

                    companyAccount3.setTitle("开发客户总监分成"); //公司明细标题
                    companyAccount3.setCreateTime(new Date()); //创建时间
                    companyAccount3.setMoney(-directorProfit);//支出金额
                    companyAccount3.setContent("总监："+directorEmployee.getName()+","+directorEmployee.getPhone()+" 分得提成 "+ directorProfit);//记账内容
                    companyAccountMapper.insert(companyAccount3);

                    //存入【总监】 个人流水账 表
                    PersonalMoney personalMoneyEmp3 = new PersonalMoney();
                    personalMoneyEmp3.setTitle("员工开发客户");
                    personalMoneyEmp3.setCreateTime(new Date());
                    personalMoneyEmp3.setContent(customerRecord.getDeveloper()+"开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle());
                    personalMoneyEmp3.setType("1");//0 客户[分公司、合伙人]   1 员工
                    personalMoneyEmp3.setPersonId(directorEmployee.getId());//个人id【流水账主人】
                    personalMoneyEmp3.setMoney(directorProfit);//金额
                    personalMoneyMapper.insert(personalMoneyEmp3);



                }
            }


        }
        else if ("1".equals(customerRecord.getType()))//分公司开发
        {
            String companyId = customerRecord.getDeveloperId();//分公司的id
            Customer branchCompany = customerMapper.selectById(companyId);//分公司对象【客户表中的对象】
            //分别查找大分公司详情，小分公司详情
            QueryWrapper<CustomerSmallCompany> smallQuery = new QueryWrapper<>();
            smallQuery.eq("customer_id",companyId);
            CustomerSmallCompany smallCompany = customerSmallCompanyMapper.selectOne(smallQuery);//小公司详情
            QueryWrapper<CustomerBigCompany> bigQuery = new QueryWrapper<>();
            bigQuery.eq("customer_id",companyId);
            CustomerBigCompany bigCompany =customerBigCompanyMapper.selectOne(bigQuery);//大公司详情


            //①.先判断该客户是不是第一次购买课程
            QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("phone", customerRecord.getPhone());
            Customer customer = customerMapper.selectOne(queryWrapper);
            if (customer == null) {//首次购买【将购买记录的数据存入客户表】
                Customer firstCustomer = new Customer();//这里修改过 ，将firstCustomer 修改成 customer
                customer = new Customer();
                List<String> pTitle = new ArrayList<>();
                pTitle.add(customerRecord.getProjectTitle());
                List<String> pId = new ArrayList<>();
                pId.add(customerRecord.getProjectId());
                String titleList = JsonUtils.serialize(pTitle);
                String idList = JsonUtils.serialize(pId);

                customer.setProjectId(idList);//已购买项目id数组
                customer.setProjectTitle(titleList);//已购买项目名称数组
                customer.setName(customerRecord.getName());//设置客户姓名
                customer.setWxNumber(customerRecord.getWxNumber());//设置微信号
                customer.setPhone(customerRecord.getPhone());   //设置微信号
                customer.setGender(customerRecord.getGender());  //设置性别
                customer.setPosition(customerRecord.getPosition());//职位
                customer.setRegion(customerRecord.getRegion()); //地区
                customer.setDeveloperId(customerRecord.getDeveloperId());//开发人id
                //设置 上级分公司id
                customer.setBranchCompanyId(customerRecord.getDeveloperId());
                customer.setType("1"); //0 员工开发    1分公司开发

                customer.setPayment(customerRecord.getPayment());//设置是否全款
                customer.setChildrenAge(customerRecord.getChildrenAge());
                customer.setOrganization(customerRecord.getOrganization());
                customer.setBusinessProject(customerRecord.getBusinessProject());
                customer.setTeacherNumber(customerRecord.getTeacherNumber());
                customer.setInstitutionalProblem(customerRecord.getInstitutionalProblem());
                customer.setHaveLessons(customerRecord.getHaveLessons());
                customer.setIdNumber(customerRecord.getIdNumber());
                customer.setBranchAttribute(customerRecord.getBranchAttribute());
                customer.setBalance(customerRecord.getBalance());
                customer.setBankNumber(customerRecord.getBankNumber());
                customer.setAlipay(customerRecord.getAlipay());
                // 设置好信息之后，插入到表中
                customerMapper.insert(customer);
                //TODO 分公司开发的都给事业部

                    //TODO ！！！！！！！！！！！！！！！！！！！！事业部分配分公司开发的客户！！！！！！！！！！！！！！！！！！！！！！给事业部总监发消息  待完成*************分配客户给员工************
                    //新建转交客户的消息【这里仅仅是名字有vip，功能可以转 分公司】
                    HandOverVip handOverVip = new HandOverVip();
                    handOverVip.setDel("0");
                    handOverVip.setState("0");
                    handOverVip.setEmployeeId(customerRecord.getDeveloperId());
                    handOverVip.setEmployeeName(customerRecord.getDeveloper());
                    handOverVip.setProjectId(customerRecord.getProjectId());//分配时用来区分，修改哪个负责人字段
                    handOverVip.setVipCustomerId(customer.getId());//购买客户的id
                    handOverVip.setVipCustomerName(customer.getName());//购买客户的姓名
                    handOverVipMapper.insert(handOverVip);//插入消息详情
                    //新建消息中心的消息
                    PersonalMessage personalMessage1 = new PersonalMessage();
                    personalMessage1.setDepartment("事业部");
                    personalMessage1.setSenderId(customerRecord.getDeveloperId());//发送者id
                    personalMessage1.setSenderName(customerRecord.getDeveloper());//发送者姓名
                    personalMessage1.setMessageType("转交分公司开发客户");//消息类型
                    personalMessage1.setState("0");//0 未读  1 已读
                    personalMessage1.setMessageLink(handOverVip.getId());
                    personalMessage1.setReceiverName("事业部");
                    personalMessage1.setReceiverId("事业部");
                    personalMessage1.setCreateTime(new Date());
                    personalMessageMapper.insert(personalMessage1);


            } else {//多次购买
                //获取原来购买的项目，然后把新购买的项目添加进去
                List<String> newProjectId = JsonUtils.parseList(customer.getProjectId(), String.class);
                List<String> newProjectTitle = JsonUtils.parseList(customer.getProjectTitle(), String.class);
                newProjectId.add(customerRecord.getProjectId());
                newProjectTitle.add(customerRecord.getProjectTitle());
                String idList = JsonUtils.serialize(newProjectId);
                String titleList = JsonUtils.serialize(newProjectTitle);
//                设置要更新修改的信息字段
                customer.setProjectId(idList);
                customer.setProjectTitle(titleList);
                customer.setPayment(customerRecord.getPayment());//设置是否全款
                customer.setChildrenAge(customerRecord.getChildrenAge());
                customer.setOrganization(customerRecord.getOrganization());
                customer.setBusinessProject(customerRecord.getBusinessProject());
                customer.setTeacherNumber(customerRecord.getTeacherNumber());
                customer.setInstitutionalProblem(customerRecord.getInstitutionalProblem());
                customer.setHaveLessons(customerRecord.getHaveLessons());
                customer.setIdNumber(customerRecord.getIdNumber());
                customer.setBranchAttribute(customerRecord.getBranchAttribute());
                customer.setBalance(customerRecord.getBalance());
                customer.setBankNumber(customerRecord.getBankNumber());
                customer.setAlipay(customerRecord.getAlipay());

                //TODO 分公司开发的都给事业部
                //TODO ！！！！！！！！！！！！！！！！！！！！事业部分配分公司开发的客户！！！！！！！！！！！！！！！！！！！！！！给事业部总监发消息  待完成*************分配客户给员工************
                   //新建转交客户的消息【这里仅仅是名字有vip，功能可以转 分公司】
                   HandOverVip handOverVip = new HandOverVip();
                     handOverVip.setEmployeeId(customerRecord.getDeveloperId());
                     handOverVip.setEmployeeName(customerRecord.getDeveloper());

                   handOverVip.setProjectId(customerRecord.getProjectId());//分配时用来区分，修改哪个负责人字段
                   handOverVip.setDel("0");
                  handOverVip.setState("0");
                   handOverVip.setVipCustomerId(customer.getId());//购买客户的id
                   handOverVip.setVipCustomerName(customer.getName());//购买客户的姓名
                   handOverVipMapper.insert(handOverVip);//插入消息详情

                   //新建消息中心的消息
                   PersonalMessage personalMessage1 = new PersonalMessage();
                   personalMessage1.setDepartment("事业部");
                   personalMessage1.setSenderId(customerRecord.getDeveloperId());//发送者id
                   personalMessage1.setSenderName(customerRecord.getDeveloper());//发送者姓名
                   personalMessage1.setMessageType("转交分公司开发客户");//消息类型
                   personalMessage1.setState("0");//0 未读  1 已读
                   personalMessage1.setMessageLink(handOverVip.getId());
                   personalMessage1.setReceiverName("事业部");
                   personalMessage1.setReceiverId("事业部");
                   personalMessage1.setCreateTime(new Date());
                   personalMessageMapper.insert(personalMessage1);
                //更新客户个人资料
                customerMapper.updateById(customer);


            }


            //②开发客户的购买项目分类
            switch (categoryId){
                case "1": //课程级别
                    //有小分公司[有赠送课程]
                    if (smallCompany != null && smallCompany.getCount()>0 && project.getId().equals(smallCompany.getProjectId())){
                        //存入记录，次月5号审核
                        SmallCompanyRecord smallCompanyRecord = new SmallCompanyRecord();
                        smallCompanyRecord.setCompanyId(companyId);//分公司id（开发者id）
                        smallCompanyRecord.setCreateTime(new Date());
                        smallCompanyRecord.setCustomerId(customer.getId());//客户id
                        smallCompanyRecord.setCustomerName(customer.getName());
                        smallCompanyRecord.setProjectId(project.getId());
                        smallCompanyRecord.setRefund(project.getMoney());//记录 需要返还的 金额
                        smallCompanyRecordMapper.insert(smallCompanyRecord);
                        //减去套餐数量
                        smallCompany.setCount(smallCompany.getCount()-1);

                        customerSmallCompanyMapper.updateById(smallCompany);

                        //通知分公司一声
                        Notice notice = new Notice();
                        notice.setContent("开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle()+" 次月计算提成 ");//通知消息内容
                        noticeMapper.insert(notice);

                        PersonalMessage personalMessage1 = new PersonalMessage();
                        personalMessage1.setMessageType("通知消息");
                        personalMessage1.setSenderId("系统");
                        personalMessage1.setSenderName("系统");
                        personalMessage1.setMessageLink(notice.getId());
                        personalMessage1.setCreateTime(new Date());
                        personalMessage1.setState("0");//消息状态 0未读
                        personalMessage1.setReceiverId(companyId);//分公司id
                        personalMessage1.setReceiverName(branchCompany.getName());

                        personalMessageMapper.insert(personalMessage1);


                        break;
                    }
                    //如果大小公司都有，按大公司的提成算
                    if (bigCompany != null){
                        //大公司提成
                        Double profit = project.getBigCompanyProfit();
                        Double bigCompanyProfit = project.getMoney() * profit;
                        //1.记录分公司金额明细中，2.并且给该分公司发消息通知一下
                        //存入 个人流水账 表
                        PersonalMoney personalMoney = new PersonalMoney();
                        personalMoney.setTitle("开发客户提成");
                        personalMoney.setCreateTime(new Date());
                        personalMoney.setContent("开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle());
                        personalMoney.setType("0");//0 客户[分公司、合伙人]   1 员工
                        personalMoney.setPersonId(companyId);//个人id【流水账主人】
                        personalMoney.setMoney(bigCompanyProfit);
                        personalMoneyMapper.insert(personalMoney);

                        //通知分公司一声
                        Notice notice = new Notice();
                        notice.setContent("开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle()+" 提成: "+bigCompanyProfit);//通知消息内容
                        noticeMapper.insert(notice);
                        PersonalMessage personalMessage1 = new PersonalMessage();
                        personalMessage1.setMessageType("通知消息");
                        personalMessage1.setSenderId("系统");
                        personalMessage1.setSenderName("系统");
                        personalMessage1.setMessageLink(notice.getId());
                        personalMessage1.setCreateTime(new Date());
                        personalMessage1.setState("0");//消息状态 0未读
                        personalMessage1.setReceiverId(companyId);//分公司id
                        personalMessage1.setReceiverName(branchCompany.getName());

                        personalMessageMapper.insert(personalMessage1);


                        //给公司总账，添加一条明细记录
                        CompanyAccount companyAccount1 = new CompanyAccount();
                        companyAccount1.setTitle("分公司开发客户分成");            //公司明细标题
                        companyAccount1.setCreateTime(new Date());       //创建时间
                        companyAccount1.setMoney(-bigCompanyProfit);//支出金额
                        companyAccount1.setContent("分公司："+branchCompany.getName()+","+branchCompany.getPhone()+" 分得提成 "+ bigCompanyProfit);//记账内容
                        companyAccountMapper.insert(companyAccount1);
                        break;
                    }
                    //如果能执行到这一步，说明是小公司，而且套餐也用完了。那么就计算提成
                    if (smallCompany != null){
                        //小公司提成
                        Double profit = project.getSmallCompanyProfit();
                        Double smallCompanyProfit = project.getMoney() * profit;
                        //1.记录分公司金额明细中，2.并且给该分公司发消息通知一下
                        //存入 个人流水账 表
                        PersonalMoney personalMoney = new PersonalMoney();
                        personalMoney.setTitle("开发客户提成");
                        personalMoney.setCreateTime(new Date());
                        personalMoney.setContent("开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle());
                        personalMoney.setType("0");//0 客户[分公司、合伙人]   1 员工
                        personalMoney.setPersonId(companyId);//个人id【流水账主人】
                        personalMoney.setMoney(smallCompanyProfit);
                        personalMoneyMapper.insert(personalMoney);

                        //通知分公司一声
                        Notice notice = new Notice();
                        notice.setContent("开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle()+" 提成: "+smallCompanyProfit);//通知消息内容
                        noticeMapper.insert(notice);
                        PersonalMessage personalMessage1 = new PersonalMessage();
                        personalMessage1.setMessageType("通知消息");
                        personalMessage1.setSenderId("系统");
                        personalMessage1.setSenderName("系统");
                        personalMessage1.setMessageLink(notice.getId());
                        personalMessage1.setCreateTime(new Date());
                        personalMessage1.setState("0");//消息状态 0未读
                        personalMessage1.setReceiverId(companyId);//分公司id
                        personalMessage1.setReceiverName(branchCompany.getName());

                        personalMessageMapper.insert(personalMessage1);


                        //给公司总账，添加一条明细记录
                        CompanyAccount companyAccount1 = new CompanyAccount();
                        companyAccount1.setTitle("分公司开发客户分成");            //公司明细标题
                        companyAccount1.setCreateTime(new Date());       //创建时间
                        companyAccount1.setMoney(-smallCompanyProfit);//支出金额
                        companyAccount1.setContent("分公司："+branchCompany.getName()+","+branchCompany.getPhone()+" 分得提成 "+ smallCompanyProfit);//记账内容
                        companyAccountMapper.insert(companyAccount1);
                        break;
                    }

                    break;
                case "2": //Vip级别
                    //有大公司[有课程款]
                    if (bigCompany != null && bigCompany.getCourseAccount()>0){
                        //存入记录   次月5号审核
                        BigCompanyRecord bigCompanyRecord = new BigCompanyRecord();
                        bigCompanyRecord.setCompanyId(companyId);
                        bigCompanyRecord.setCreateTime(new Date());
                        bigCompanyRecord.setCustomerId(customer.getId());
                        bigCompanyRecord.setCustomerName(customer.getName());
                        bigCompanyRecord.setProjectId(project.getId());

                        //减去 课程款
                        Double account = bigCompany.getCourseAccount() - project.getMoney();
                        if (account<0){
                            bigCompanyRecord.setRefund(bigCompany.getCourseAccount());//记录 需要返还的 金额
                            bigCompany.setCourseAccount(0.0);  //设置余额
                        }else{
                            bigCompanyRecord.setRefund(project.getMoney());//记录 需要返还的 金额
                            bigCompany.setCourseAccount(account); //设置余额
                        }

                        //存入开发记录
                        bigCompanyRecordMapper.insert(bigCompanyRecord);
                        //更新 套餐余额
                        customerBigCompanyMapper.insert(bigCompany);
                        //通知分公司一声
                        Notice notice = new Notice();
                        notice.setContent("开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle()+" 次月计算提成 ");//通知消息内容
                        noticeMapper.insert(notice);
                        PersonalMessage personalMessage1 = new PersonalMessage();
                        personalMessage1.setMessageType("通知消息");
                        personalMessage1.setSenderId("系统");
                        personalMessage1.setSenderName("系统");
                        personalMessage1.setMessageLink(notice.getId());
                        personalMessage1.setCreateTime(new Date());
                        personalMessage1.setState("0");//消息状态 0未读
                        personalMessage1.setReceiverId(companyId);//分公司id
                        personalMessage1.setReceiverName(branchCompany.getName());

                        personalMessageMapper.insert(personalMessage1);

                        break;


                    }

                    //如果有大公司身份，优先按大公司算提成【当前情况为，大公司课程款已用完】
                    if (bigCompany != null){
                        //大公司提成
                        Double profit = project.getBigCompanyProfit();
                        Double bigCompanyProfit = project.getMoney() * profit;
                        //1.记录分公司金额明细中，2.并且给该分公司发消息通知一下
                        //存入 个人流水账 表
                        PersonalMoney personalMoney = new PersonalMoney();
                        personalMoney.setTitle("开发客户提成");
                        personalMoney.setCreateTime(new Date());
                        personalMoney.setContent("开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle());
                        personalMoney.setType("0");//0 客户[分公司、合伙人]   1 员工
                        personalMoney.setPersonId(companyId);//个人id【流水账主人】
                        personalMoney.setMoney(bigCompanyProfit);
                        personalMoneyMapper.insert(personalMoney);

                        //通知分公司一声
                        Notice notice = new Notice();
                        notice.setContent("开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle()+" 提成: "+bigCompanyProfit);//通知消息内容
                        noticeMapper.insert(notice);
                        PersonalMessage personalMessage1 = new PersonalMessage();
                        personalMessage1.setMessageType("通知消息");
                        personalMessage1.setSenderId("系统");
                        personalMessage1.setSenderName("系统");
                        personalMessage1.setMessageLink(notice.getId());
                        personalMessage1.setCreateTime(new Date());
                        personalMessage1.setState("0");//消息状态 0未读
                        personalMessage1.setReceiverId(companyId);//分公司id
                        personalMessage1.setReceiverName(branchCompany.getName());

                        personalMessageMapper.insert(personalMessage1);


                        //给公司总账，添加一条明细记录
                        CompanyAccount companyAccount1 = new CompanyAccount();
                        companyAccount1.setTitle("分公司开发客户分成");            //公司明细标题
                        companyAccount1.setCreateTime(new Date());       //创建时间
                        companyAccount1.setMoney(-bigCompanyProfit);//支出金额
                        companyAccount1.setContent("分公司："+branchCompany.getName()+","+branchCompany.getPhone()+" 分得提成 "+ bigCompanyProfit);//记账内容
                        companyAccountMapper.insert(companyAccount1);
                        break;
                    }
                    //如果能走到这一步，说明这就是个小公司
                    if (smallCompany != null){
                        //小公司提成
                        Double profit = project.getSmallCompanyProfit();
                        Double smallCompanyProfit = project.getMoney() * profit;
                        //1.记录分公司金额明细中，2.并且给该分公司发消息通知一下
                        //存入 个人流水账 表
                        PersonalMoney personalMoney = new PersonalMoney();
                        personalMoney.setTitle("开发客户提成");
                        personalMoney.setCreateTime(new Date());
                        personalMoney.setContent("开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle());
                        personalMoney.setType("0");//0 客户[分公司、合伙人]   1 员工
                        personalMoney.setPersonId(companyId);//个人id【流水账主人】
                        personalMoney.setMoney(smallCompanyProfit);
                        personalMoneyMapper.insert(personalMoney);

                        //通知分公司一声
                        Notice notice = new Notice();
                        notice.setContent("开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle()+" 提成: "+smallCompanyProfit);//通知消息内容
                        noticeMapper.insert(notice);
                        PersonalMessage personalMessage1 = new PersonalMessage();
                        personalMessage1.setMessageType("通知消息");
                        personalMessage1.setSenderId("系统");
                        personalMessage1.setSenderName("系统");
                        personalMessage1.setMessageLink(notice.getId());
                        personalMessage1.setCreateTime(new Date());
                        personalMessage1.setState("0");//消息状态 0未读
                        personalMessage1.setReceiverId(companyId);//分公司id
                        personalMessage1.setReceiverName(branchCompany.getName());

                        personalMessageMapper.insert(personalMessage1);

                        //给公司总账，添加一条明细记录
                        CompanyAccount companyAccount1 = new CompanyAccount();
                        companyAccount1.setTitle("分公司开发客户分成");            //公司明细标题
                        companyAccount1.setCreateTime(new Date());       //创建时间
                        companyAccount1.setMoney(-smallCompanyProfit);//支出金额
                        companyAccount1.setContent("分公司："+branchCompany.getName()+","+branchCompany.getPhone()+" 分得提成 "+ smallCompanyProfit);//记账内容
                        companyAccountMapper.insert(companyAccount1);

                        //如果这个小分公司【当前开发人】还有上级，再计算给上级的比例
                        if("1".equals(branchCompany.getType()) && branchCompany.getBranchCompanyId()!= null){
                            //这里的服务老师暂定为，小分公司的 负责人

//                            branchCompany.getBranchCompanyId();//上级公司 [客户身份]
                            Customer companySuper = customerMapper.selectById(branchCompany.getBranchCompanyId());
//                            branchCompany.getPrincipalCompanyId();//负责人 [员工身份]
                            Employee companyTeacher = employeeMapper.selectById(branchCompany.getPrincipalCompanyId());
                            //计算提成比例
                            Double gap = project.getBigCompanyProfit() - project.getSmallCompanyProfit();
                            Double superProfit = gap * branchProfitConfig.getCorporationElectCompanyElectVipBig();
                            Double teacherProfit = gap * branchProfitConfig.getCorporationElectCompanyElectVipTeacher();
                            //给上级大分公司记录提成
                                //存入 个人流水账 表
                                PersonalMoney personalMoneySuper = new PersonalMoney();
                                personalMoneySuper.setTitle("下级小分公司推荐了Vip");
                                personalMoneySuper.setCreateTime(new Date());
                                personalMoneySuper.setContent(branchCompany.getName()+","+branchCompany.getPhone()+" 开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle());
                                personalMoneySuper.setType("0");//0 客户[分公司、合伙人]   1 员工
                                personalMoneySuper.setPersonId(branchCompany.getBranchCompanyId());//个人id【流水账主人】
                                personalMoneySuper.setMoney(superProfit);//金额
                                personalMoneyMapper.insert(personalMoneySuper);
                            //给当前服务老师记录提成
                                //TODO 全部流水账改成这个模式
                                //存入 个人流水账 表
                                PersonalMoney personalMoneyTeacher = new PersonalMoney();
                                personalMoneyTeacher.setTitle("小分公司推荐了Vip");
                                personalMoneyTeacher.setCreateTime(new Date());
                                personalMoneyTeacher.setContent(branchCompany.getName()+","+branchCompany.getPhone()+" 开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle());
                                personalMoneyTeacher.setType("1");//0 客户[分公司、合伙人]   1 员工
                                personalMoneyTeacher.setPersonId(branchCompany.getPrincipalCompanyId());//个人id【流水账主人】
                                personalMoneyTeacher.setMoney(teacherProfit);//金额
                                personalMoneyMapper.insert(personalMoneyTeacher);
                            //公司总账减去 上面给出的提成
                                //给公司总账，添加一条明细记录
                                CompanyAccount companyAccountSuper = new CompanyAccount();
                                companyAccountSuper.setTitle("小分公司开发Vip，给上级提成");            //公司明细标题
                                companyAccountSuper.setCreateTime(new Date());       //创建时间
                                companyAccountSuper.setMoney(-superProfit);//支出金额
                                companyAccountSuper.setContent("上级分公司："+companySuper.getName()+","+companySuper.getPhone()+" 分得提成 "+ superProfit);//记账内容
                                companyAccountMapper.insert(companyAccountSuper);
                                //给公司总账，添加一条明细记录
                                CompanyAccount companyAccountTeacher = new CompanyAccount();
                                companyAccountTeacher.setTitle("小分公司开发Vip，给服务老师提成");            //公司明细标题
                                companyAccountTeacher.setCreateTime(new Date());       //创建时间
                                companyAccountTeacher.setMoney(-teacherProfit);//支出金额
                                companyAccountTeacher.setContent("服务老师："+companyTeacher.getName()+","+companyTeacher.getPhone()+" 分得提成 "+ teacherProfit);//记账内容
                                companyAccountMapper.insert(companyAccountTeacher);
                        }

                        break;
                    }

                    break;
                case "3": //小分公司
                    //如果大小分公司都有，优先大分公司身份
                    if (bigCompany != null){
                        //大公司直推小分公司提成
                        Double bigCompanyProfit = branchProfitConfig.getCorporationElectCompany();
                        //1.记录分公司金额明细中，2.并且给该分公司发消息通知一下
                        //存入 个人流水账 表
                        PersonalMoney personalMoney = new PersonalMoney();
                        personalMoney.setTitle("开发客户提成");
                        personalMoney.setCreateTime(new Date());
                        personalMoney.setContent("开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle());
                        personalMoney.setType("0");//0 客户[分公司、合伙人]   1 员工
                        personalMoney.setPersonId(companyId);//个人id【流水账主人】
                        personalMoney.setMoney(bigCompanyProfit);
                        personalMoneyMapper.insert(personalMoney);

                        //通知分公司一声
                        Notice notice = new Notice();
                        notice.setContent("开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle()+" 提成: "+bigCompanyProfit);//通知消息内容
                        noticeMapper.insert(notice);
                        PersonalMessage personalMessage1 = new PersonalMessage();
                        personalMessage1.setMessageType("通知消息");
                        personalMessage1.setSenderId("系统");
                        personalMessage1.setSenderName("系统");
                        personalMessage1.setMessageLink(notice.getId());
                        personalMessage1.setCreateTime(new Date());
                        personalMessage1.setState("0");//消息状态 0未读
                        personalMessage1.setReceiverId(companyId);//分公司id
                        personalMessage1.setReceiverName(branchCompany.getName());

                        personalMessageMapper.insert(personalMessage1);

                        //给公司总账，添加一条明细记录
                        CompanyAccount companyAccount1 = new CompanyAccount();
                        companyAccount1.setTitle("分公司开发客户分成");            //公司明细标题
                        companyAccount1.setCreateTime(new Date());       //创建时间
                        companyAccount1.setMoney(-bigCompanyProfit);//支出金额
                        companyAccount1.setContent("分公司："+branchCompany.getName()+","+branchCompany.getPhone()+" 分得提成 "+ bigCompanyProfit);//记账内容
                        companyAccountMapper.insert(companyAccount1);
                        break;
                    }
                    //小分公司
                    if (smallCompany != null){
                        //小公司直推小分公司提成
                        Double smallCompanyProfit =branchProfitConfig.getCompanyElectCompany();
                        //1.记录分公司金额明细中，2.并且给该分公司发消息通知一下
                        //存入 个人流水账 表
                        PersonalMoney personalMoney = new PersonalMoney();
                        personalMoney.setTitle("开发客户提成");
                        personalMoney.setCreateTime(new Date());
                        personalMoney.setContent("开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle());
                        personalMoney.setType("0");//0 客户[分公司、合伙人]   1 员工
                        personalMoney.setPersonId(companyId);//个人id【流水账主人】
                        personalMoney.setMoney(smallCompanyProfit);
                        personalMoneyMapper.insert(personalMoney);

                        //通知分公司一声
                        Notice notice = new Notice();
                        notice.setContent("开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle()+" 提成: "+smallCompanyProfit);//通知消息内容
                        noticeMapper.insert(notice);
                        PersonalMessage personalMessage1 = new PersonalMessage();
                        personalMessage1.setMessageType("通知消息");
                        personalMessage1.setSenderId("系统");
                        personalMessage1.setSenderName("系统");
                        personalMessage1.setMessageLink(notice.getId());
                        personalMessage1.setCreateTime(new Date());
                        personalMessage1.setState("0");//消息状态 0未读
                        personalMessage1.setReceiverId(companyId);//分公司id
                        personalMessage1.setReceiverName(branchCompany.getName());

                        personalMessageMapper.insert(personalMessage1);

                        //给公司总账，添加一条明细记录
                        CompanyAccount companyAccount1 = new CompanyAccount();
                        companyAccount1.setTitle("分公司开发客户分成");  //公司明细标题
                        companyAccount1.setCreateTime(new Date());    //创建时间
                        companyAccount1.setMoney(-smallCompanyProfit);//支出金额
                        companyAccount1.setContent("分公司："+branchCompany.getName()+","+branchCompany.getPhone()+" 分得提成 "+ smallCompanyProfit);//记账内容
                        companyAccountMapper.insert(companyAccount1);
                        break;
                    }

                    break;
                case "4": //大分公司
                    //大分公司
                    if (bigCompany != null){
                        //大公司直推大分公司提成
                        Double bigCompanyProfit = branchProfitConfig.getCorporationElectCorporation();
                        //1.记录分公司金额明细中，2.并且给该分公司发消息通知一下
                        //存入 个人流水账 表
                        PersonalMoney personalMoney = new PersonalMoney();
                        personalMoney.setTitle("开发客户提成");
                        personalMoney.setCreateTime(new Date());
                        personalMoney.setContent("开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle());
                        personalMoney.setType("0");//0 客户[分公司、合伙人]   1 员工
                        personalMoney.setPersonId(companyId);//个人id【流水账主人】
                        personalMoney.setMoney(bigCompanyProfit);
                        personalMoneyMapper.insert(personalMoney);

                        //通知分公司一声
                        Notice notice = new Notice();
                        notice.setContent("开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle()+" 提成: "+bigCompanyProfit);//通知消息内容
                        noticeMapper.insert(notice);
                        PersonalMessage personalMessage1 = new PersonalMessage();
                        personalMessage1.setMessageType("通知消息");
                        personalMessage1.setSenderId("系统");
                        personalMessage1.setSenderName("系统");
                        personalMessage1.setMessageLink(notice.getId());
                        personalMessage1.setCreateTime(new Date());
                        personalMessage1.setState("0");//消息状态 0未读
                        personalMessage1.setReceiverId(companyId);//分公司id
                        personalMessage1.setReceiverName(branchCompany.getName());

                        personalMessageMapper.insert(personalMessage1);

                        //给公司总账，添加一条明细记录
                        CompanyAccount companyAccount1 = new CompanyAccount();
                        companyAccount1.setTitle("分公司开发客户分成");            //公司明细标题
                        companyAccount1.setCreateTime(new Date());       //创建时间
                        companyAccount1.setMoney(-bigCompanyProfit);//支出金额
                        companyAccount1.setContent("分公司："+branchCompany.getName()+","+branchCompany.getPhone()+" 分得提成 "+ bigCompanyProfit);//记账内容
                        companyAccountMapper.insert(companyAccount1);
                        break;
                    }
                    //小分公司
                    if (smallCompany != null){
                        //小公司直推大分公司提成
                        Double smallCompanyProfit =branchProfitConfig.getCompanyElectCorporation();
                        //1.记录分公司金额明细中，2.并且给该分公司发消息通知一下
                        //存入 个人流水账 表
                        PersonalMoney personalMoney = new PersonalMoney();
                        personalMoney.setTitle("开发客户提成");
                        personalMoney.setCreateTime(new Date());
                        personalMoney.setContent("开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle());
                        personalMoney.setType("0");//0 客户[分公司、合伙人]   1 员工
                        personalMoney.setPersonId(companyId);//个人id【流水账主人】
                        personalMoney.setMoney(smallCompanyProfit);
                        personalMoneyMapper.insert(personalMoney);

                        //通知分公司一声
                        Notice notice = new Notice();
                        notice.setContent("开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle()+" 提成: "+smallCompanyProfit);//通知消息内容
                        noticeMapper.insert(notice);
                        PersonalMessage personalMessage1 = new PersonalMessage();
                        personalMessage1.setMessageType("通知消息");
                        personalMessage1.setSenderId("系统");
                        personalMessage1.setSenderName("系统");
                        personalMessage1.setMessageLink(notice.getId());
                        personalMessage1.setCreateTime(new Date());
                        personalMessage1.setState("0");//消息状态 0未读
                        personalMessage1.setReceiverId(companyId);//分公司id
                        personalMessage1.setReceiverName(branchCompany.getName());

                        personalMessageMapper.insert(personalMessage1);

                        //给公司总账，添加一条明细记录
                        CompanyAccount companyAccount1 = new CompanyAccount();
                        companyAccount1.setTitle("分公司开发客户分成");  //公司明细标题
                        companyAccount1.setCreateTime(new Date());    //创建时间
                        companyAccount1.setMoney(-smallCompanyProfit);//支出金额
                        companyAccount1.setContent("分公司："+branchCompany.getName()+","+branchCompany.getPhone()+" 分得提成 "+ smallCompanyProfit);//记账内容
                        companyAccountMapper.insert(companyAccount1);
                        break;
                    }

                    //如果是分公司开发，并且上级分公司id 不为空
                    if ("1".equals(customer.getType()) && StringUtils.isNotBlank(customer.getBranchCompanyId())){
                        //判断上级是 大分公司  还是  小分公司 计算对应提成
                        String superId = customer.getBranchCompanyId();//上级分公司id
                        Customer superCompany = customerMapper.selectById(superId);//上级公司对象
                        QueryWrapper<CustomerSmallCompany> smallQ = new QueryWrapper<>();
                        smallQ.eq("customer_id",superId);
                        CustomerSmallCompany small = customerSmallCompanyMapper.selectOne(smallQ);//小公司详情
                        QueryWrapper<CustomerBigCompany> bigQ = new QueryWrapper<>();
                        bigQ.eq("customer_id",superId);
                        CustomerBigCompany big = customerBigCompanyMapper.selectOne(bigQ);//大公司详情
                        //如果上级是大分公司
                        if (big != null){
                            Double bigProfit = branchProfitConfig.getCorporationElectCompanyUpgrade();

                            //存入【上级大分公司】 个人流水账 表
                            PersonalMoney personalMoney = new PersonalMoney();
                            personalMoney.setTitle("下级开发客户提成");
                            personalMoney.setCreateTime(new Date());
                            personalMoney.setContent("下级"+customerRecord.getDeveloper()+" 开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle());
                            personalMoney.setType("0");//0 客户[分公司、合伙人]   1 员工
                            personalMoney.setPersonId(superId);//个人id【流水账主人】
                            personalMoney.setMoney(bigProfit);
                            personalMoneyMapper.insert(personalMoney);

                            //通知上级分公司一声
                            Notice notice = new Notice();
                            notice.setContent("下级"+customerRecord.getDeveloper()+" 开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle()+" 提成: "+bigProfit);//通知消息内容
                            noticeMapper.insert(notice);
                            PersonalMessage personalMessage1 = new PersonalMessage();
                            personalMessage1.setMessageType("通知消息");
                            personalMessage1.setSenderId("系统");
                            personalMessage1.setSenderName("系统");
                            personalMessage1.setMessageLink(notice.getId());
                            personalMessage1.setCreateTime(new Date());
                            personalMessage1.setState("0");//消息状态 0未读
                            personalMessage1.setReceiverId(superId);//上级分公司id
                            personalMessage1.setReceiverName(superCompany.getName());

                            personalMessageMapper.insert(personalMessage1);

                            //给公司总账，添加一条明细记录
                            CompanyAccount companyAccount1 = new CompanyAccount();
                            companyAccount1.setTitle("分公司开发客户,上级分成");            //公司明细标题
                            companyAccount1.setCreateTime(new Date());       //创建时间
                            companyAccount1.setMoney(-bigProfit);//支出金额
                            companyAccount1.setContent("分公司："+superCompany.getName()+","+superCompany.getPhone()+" 分得提成 "+ bigProfit);//记账内容
                            companyAccountMapper.insert(companyAccount1);
                            break;
                        }
                        //如果上级是小分公司
                        if (small != null){
                            Double smallProfit = branchProfitConfig.getCompanyElectCompanyUpgrade();

                            //存入【上级小分公司】 个人流水账 表
                            PersonalMoney personalMoney = new PersonalMoney();
                            personalMoney.setTitle("下级开发客户提成");
                            personalMoney.setCreateTime(new Date());
                            personalMoney.setContent("下级"+customerRecord.getDeveloper()+" 开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle());
                            personalMoney.setType("0");//0 客户[分公司、合伙人]   1 员工
                            personalMoney.setPersonId(superId);//个人id【流水账主人】
                            personalMoney.setMoney(smallProfit);
                            personalMoneyMapper.insert(personalMoney);

                            //通知上级分公司一声
                            Notice notice = new Notice();
                            notice.setContent("下级"+customerRecord.getDeveloper()+" 开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle()+" 提成: "+smallProfit);//通知消息内容
                            noticeMapper.insert(notice);
                            PersonalMessage personalMessage1 = new PersonalMessage();
                            personalMessage1.setMessageType("通知消息");
                            personalMessage1.setSenderId("系统");
                            personalMessage1.setSenderName("系统");
                            personalMessage1.setMessageLink(notice.getId());
                            personalMessage1.setCreateTime(new Date());
                            personalMessage1.setState("0");//消息状态 0未读
                            personalMessage1.setReceiverId(superId);//上级分公司id
                            personalMessage1.setReceiverName(superCompany.getName());

                            personalMessageMapper.insert(personalMessage1);

                            //给公司总账，添加一条明细记录
                            CompanyAccount companyAccount1 = new CompanyAccount();
                            companyAccount1.setTitle("分公司开发客户,上级分成");            //公司明细标题
                            companyAccount1.setCreateTime(new Date());       //创建时间
                            companyAccount1.setMoney(-smallProfit);//支出金额
                            companyAccount1.setContent("分公司："+superCompany.getName()+","+superCompany.getPhone()+" 分得提成 "+ smallProfit);//记账内容
                            companyAccountMapper.insert(companyAccount1);
                            break;
                        }
                    }
                    break;
            }

        }
        else if ("2".equals(customerRecord.getType()))//事业部为分公司录入客户
        {
            //TODO 事业部为分公司录入客户的区别【1.录入员工，无论什么项目都拿3%提成，2.录入的新客户为该员工负责】
            String companyId = customerRecord.getDeveloperId();//分公司的id
            Customer branchCompany = customerMapper.selectById(companyId);//分公司对象【客户表中的对象】
            //分别查找大分公司详情，小分公司详情
            QueryWrapper<CustomerSmallCompany> smallQuery = new QueryWrapper<>();
            smallQuery.eq("customer_id",companyId);
            CustomerSmallCompany smallCompany = customerSmallCompanyMapper.selectOne(smallQuery);//小公司详情
            QueryWrapper<CustomerBigCompany> bigQuery = new QueryWrapper<>();
            bigQuery.eq("customer_id",companyId);
            CustomerBigCompany bigCompany =customerBigCompanyMapper.selectOne(bigQuery);//大公司详情


            //①.先判断该客户是不是第一次购买课程
            QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("phone", customerRecord.getPhone());
            Customer customer = customerMapper.selectOne(queryWrapper);
            if (customer == null) {//首次购买【将购买记录的数据存入客户表】
                Customer firstCustomer = new Customer();//这里修改过 ，将firstCustomer 修改成 customer
                customer = new Customer();
                List<String> pTitle = new ArrayList<>();
                pTitle.add(customerRecord.getProjectTitle());
                List<String> pId = new ArrayList<>();
                pId.add(customerRecord.getProjectId());
                String titleList = JsonUtils.serialize(pTitle);
                String idList = JsonUtils.serialize(pId);

                customer.setProjectId(idList);//已购买项目id数组
                customer.setProjectTitle(titleList);//已购买项目名称数组
                customer.setName(customerRecord.getName());//设置客户姓名
                customer.setWxNumber(customerRecord.getWxNumber());//设置微信号
                customer.setPhone(customerRecord.getPhone());   //设置微信号
                customer.setGender(customerRecord.getGender());  //设置性别
                customer.setPosition(customerRecord.getPosition());//职位
                customer.setRegion(customerRecord.getRegion()); //地区
                customer.setDeveloperId(customerRecord.getDeveloperId());//开发人id
                //设置 上级分公司id
                customer.setBranchCompanyId(customerRecord.getDeveloperId());
                customer.setType("1"); //0 员工开发    1分公司开发

                customer.setPayment(customerRecord.getPayment());//设置是否全款
                customer.setChildrenAge(customerRecord.getChildrenAge());
                customer.setOrganization(customerRecord.getOrganization());
                customer.setBusinessProject(customerRecord.getBusinessProject());
                customer.setTeacherNumber(customerRecord.getTeacherNumber());
                customer.setInstitutionalProblem(customerRecord.getInstitutionalProblem());
                customer.setHaveLessons(customerRecord.getHaveLessons());
                customer.setIdNumber(customerRecord.getIdNumber());
                customer.setBranchAttribute(customerRecord.getBranchAttribute());
                customer.setBalance(customerRecord.getBalance());
                customer.setBankNumber(customerRecord.getBankNumber());
                customer.setAlipay(customerRecord.getAlipay());
                // 设置好信息之后，插入到表中
                customerMapper.insert(customer);



            } else {//多次购买
                //获取原来购买的项目，然后把新购买的项目添加进去
                List<String> newProjectId = JsonUtils.parseList(customer.getProjectId(), String.class);
                List<String> newProjectTitle = JsonUtils.parseList(customer.getProjectTitle(), String.class);
                newProjectId.add(customerRecord.getProjectId());
                newProjectTitle.add(customerRecord.getProjectTitle());
                String idList = JsonUtils.serialize(newProjectId);
                String titleList = JsonUtils.serialize(newProjectTitle);
//                设置要更新修改的信息字段
                customer.setProjectId(idList);
                customer.setProjectTitle(titleList);
                customer.setPayment(customerRecord.getPayment());//设置是否全款
                customer.setChildrenAge(customerRecord.getChildrenAge());
                customer.setOrganization(customerRecord.getOrganization());
                customer.setBusinessProject(customerRecord.getBusinessProject());
                customer.setTeacherNumber(customerRecord.getTeacherNumber());
                customer.setInstitutionalProblem(customerRecord.getInstitutionalProblem());
                customer.setHaveLessons(customerRecord.getHaveLessons());
                customer.setIdNumber(customerRecord.getIdNumber());
                customer.setBranchAttribute(customerRecord.getBranchAttribute());
                customer.setBalance(customerRecord.getBalance());//设置 首付交了多少钱
                customer.setBankNumber(customerRecord.getBankNumber());
                customer.setAlipay(customerRecord.getAlipay());

                //更新客户个人资料
                customerMapper.updateById(customer);


            }

            //②开发客户的购买项目分类
            switch (categoryId){
                case "1": //课程级别
                    //有小分公司[有赠送课程]
                    if (smallCompany != null && smallCompany.getCount()>0 && project.getId().equals(smallCompany.getProjectId())){
                        //存入记录，次月5号审核
                        SmallCompanyRecord smallCompanyRecord = new SmallCompanyRecord();
                        smallCompanyRecord.setCompanyId(companyId);//分公司id（开发者id）
                        smallCompanyRecord.setCreateTime(new Date());
                        smallCompanyRecord.setCustomerId(customer.getId());//客户id
                        smallCompanyRecord.setCustomerName(customer.getName());
                        smallCompanyRecord.setProjectId(project.getId());
                        smallCompanyRecord.setRefund(project.getMoney());//记录 需要返还的 金额
                        smallCompanyRecordMapper.insert(smallCompanyRecord);
                        //减去套餐数量
                        smallCompany.setCount(smallCompany.getCount()-1);

                        customerSmallCompanyMapper.updateById(smallCompany);

                        //通知分公司一声
                        Notice notice = new Notice();
                        notice.setContent("开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle()+" 次月计算提成 ");//通知消息内容
                        noticeMapper.insert(notice);
                        PersonalMessage personalMessage1 = new PersonalMessage();
                        personalMessage1.setMessageType("通知消息");
                        personalMessage1.setSenderId("系统");
                        personalMessage1.setSenderName("系统");
                        personalMessage1.setMessageLink(notice.getId());
                        personalMessage1.setCreateTime(new Date());
                        personalMessage1.setState("0");//消息状态 0未读
                        personalMessage1.setReceiverId(companyId);//分公司id
                        personalMessage1.setReceiverName(branchCompany.getName());

                        personalMessageMapper.insert(personalMessage1);


                        break;
                    }
                    //如果大小公司都有，按大公司的提成算
                    if (bigCompany != null){
                        //大公司提成
                        Double profit = project.getBigCompanyProfit();
                        Double bigCompanyProfit = project.getMoney() * profit;
                        //1.记录分公司金额明细中，2.并且给该分公司发消息通知一下
                        //存入 个人流水账 表
                        PersonalMoney personalMoney = new PersonalMoney();
                        personalMoney.setTitle("开发客户提成");
                        personalMoney.setCreateTime(new Date());
                        personalMoney.setContent("开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle());
                        personalMoney.setType("0");//0 客户[分公司、合伙人]   1 员工
                        personalMoney.setPersonId(companyId);//个人id【流水账主人】
                        personalMoney.setMoney(bigCompanyProfit);
                        personalMoneyMapper.insert(personalMoney);

                        //通知分公司一声
                        Notice notice = new Notice();
                        notice.setContent("开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle()+" 提成: "+bigCompanyProfit);//通知消息内容
                        noticeMapper.insert(notice);
                        PersonalMessage personalMessage1 = new PersonalMessage();
                        personalMessage1.setMessageType("通知消息");
                        personalMessage1.setSenderId("系统");
                        personalMessage1.setSenderName("系统");
                        personalMessage1.setMessageLink(notice.getId());
                        personalMessage1.setCreateTime(new Date());
                        personalMessage1.setState("0");//消息状态 0未读
                        personalMessage1.setReceiverId(companyId);//分公司id
                        personalMessage1.setReceiverName(branchCompany.getName());

                        personalMessageMapper.insert(personalMessage1);


                        //给公司总账，添加一条明细记录
                        CompanyAccount companyAccount1 = new CompanyAccount();
                        companyAccount1.setTitle("分公司开发客户分成");            //公司明细标题
                        companyAccount1.setCreateTime(new Date());       //创建时间
                        companyAccount1.setMoney(-bigCompanyProfit);//支出金额
                        companyAccount1.setContent("分公司："+branchCompany.getName()+","+branchCompany.getPhone()+" 分得提成 "+ bigCompanyProfit);//记账内容
                        companyAccountMapper.insert(companyAccount1);
                        break;
                    }
                    //如果能执行到这一步，说明是小公司，而且套餐也用完了。那么就计算提成
                    if (smallCompany != null){
                        //小公司提成
                        Double profit = project.getSmallCompanyProfit();
                        Double smallCompanyProfit = project.getMoney() * profit;
                        //1.记录分公司金额明细中，2.并且给该分公司发消息通知一下
                        //存入 个人流水账 表
                        PersonalMoney personalMoney = new PersonalMoney();
                        personalMoney.setTitle("开发客户提成");
                        personalMoney.setCreateTime(new Date());
                        personalMoney.setContent("开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle());
                        personalMoney.setType("0");//0 客户[分公司、合伙人]   1 员工
                        personalMoney.setPersonId(companyId);//个人id【流水账主人】
                        personalMoney.setMoney(smallCompanyProfit);
                        personalMoneyMapper.insert(personalMoney);

                        //通知分公司一声
                        Notice notice = new Notice();
                        notice.setContent("开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle()+" 提成: "+smallCompanyProfit);//通知消息内容
                        noticeMapper.insert(notice);
                        PersonalMessage personalMessage1 = new PersonalMessage();
                        personalMessage1.setMessageType("通知消息");
                        personalMessage1.setSenderId("系统");
                        personalMessage1.setSenderName("系统");
                        personalMessage1.setMessageLink(notice.getId());
                        personalMessage1.setCreateTime(new Date());
                        personalMessage1.setState("0");//消息状态 0未读
                        personalMessage1.setReceiverId(companyId);//分公司id
                        personalMessage1.setReceiverName(branchCompany.getName());

                        personalMessageMapper.insert(personalMessage);


                        //给公司总账，添加一条明细记录
                        CompanyAccount companyAccount1 = new CompanyAccount();
                        companyAccount1.setTitle("分公司开发客户分成");            //公司明细标题
                        companyAccount1.setCreateTime(new Date());       //创建时间
                        companyAccount1.setMoney(-smallCompanyProfit);//支出金额
                        companyAccount1.setContent("分公司："+branchCompany.getName()+","+branchCompany.getPhone()+" 分得提成 "+ smallCompanyProfit);//记账内容
                        companyAccountMapper.insert(companyAccount1);
                        break;
                    }

                    customer.setPrincipalCourseId(customerRecord.getDivisionEmployeeId());
                    customerMapper.updateById(customer);
                    break;
                case "2": //Vip级别
                    //有大公司[有课程款]
                    if (bigCompany != null && bigCompany.getCourseAccount()>0){
                        //存入记录   次月5号审核
                        BigCompanyRecord bigCompanyRecord = new BigCompanyRecord();
                        bigCompanyRecord.setCompanyId(companyId);
                        bigCompanyRecord.setCreateTime(new Date());
                        bigCompanyRecord.setCustomerId(customer.getId());
                        bigCompanyRecord.setCustomerName(customer.getName());
                        bigCompanyRecord.setProjectId(project.getId());

                        //减去 课程款
                        Double account = bigCompany.getCourseAccount() - project.getMoney();
                        if (account<0){
                            bigCompanyRecord.setRefund(bigCompany.getCourseAccount());//记录 需要返还的 金额
                            bigCompany.setCourseAccount(0.0);  //设置余额
                        }else{
                            bigCompanyRecord.setRefund(project.getMoney());//记录 需要返还的 金额
                            bigCompany.setCourseAccount(account); //设置余额
                        }

                        //存入开发记录
                        bigCompanyRecordMapper.insert(bigCompanyRecord);
                        //更新 套餐余额
                        customerBigCompanyMapper.updateById(bigCompany);
                        //通知分公司一声
                        Notice notice = new Notice();
                        notice.setContent("开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle()+" 次月计算提成 ");//通知消息内容
                        noticeMapper.insert(notice);
                        PersonalMessage personalMessage1 = new PersonalMessage();
                        personalMessage1.setMessageType("通知消息");
                        personalMessage1.setSenderId("系统");
                        personalMessage1.setSenderName("系统");
                        personalMessage1.setMessageLink(notice.getId());
                        personalMessage1.setCreateTime(new Date());
                        personalMessage1.setState("0");//消息状态 0未读
                        personalMessage1.setReceiverId(companyId);//分公司id
                        personalMessage1.setReceiverName(branchCompany.getName());

                        personalMessageMapper.insert(personalMessage1);

                        break;


                    }

                    //如果有大公司身份，优先按大公司算提成【当前情况为，大公司课程款已用完】
                    if (bigCompany != null){
                        //大公司提成
                        Double profit = project.getBigCompanyProfit();
                        Double bigCompanyProfit = project.getMoney() * profit;
                        //1.记录分公司金额明细中，2.并且给该分公司发消息通知一下
                        //存入 个人流水账 表
                        PersonalMoney personalMoney = new PersonalMoney();
                        personalMoney.setTitle("开发客户提成");
                        personalMoney.setCreateTime(new Date());
                        personalMoney.setContent("开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle());
                        personalMoney.setType("0");//0 客户[分公司、合伙人]   1 员工
                        personalMoney.setPersonId(companyId);//个人id【流水账主人】
                        personalMoney.setMoney(bigCompanyProfit);
                        personalMoneyMapper.insert(personalMoney);

                        //通知分公司一声
                        Notice notice = new Notice();
                        notice.setContent("开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle()+" 提成: "+bigCompanyProfit);//通知消息内容
                        noticeMapper.insert(notice);
                        PersonalMessage personalMessage1 = new PersonalMessage();
                        personalMessage1.setMessageType("通知消息");
                        personalMessage1.setSenderId("系统");
                        personalMessage1.setSenderName("系统");
                        personalMessage1.setMessageLink(notice.getId());
                        personalMessage1.setCreateTime(new Date());
                        personalMessage1.setState("0");//消息状态 0未读
                        personalMessage1.setReceiverId(companyId);//分公司id
                        personalMessage1.setReceiverName(branchCompany.getName());

                        personalMessageMapper.insert(personalMessage1);


                        //给公司总账，添加一条明细记录
                        CompanyAccount companyAccount1 = new CompanyAccount();
                        companyAccount1.setTitle("分公司开发客户分成");            //公司明细标题
                        companyAccount1.setCreateTime(new Date());       //创建时间
                        companyAccount1.setMoney(-bigCompanyProfit);//支出金额
                        companyAccount1.setContent("分公司："+branchCompany.getName()+","+branchCompany.getPhone()+" 分得提成 "+ bigCompanyProfit);//记账内容
                        companyAccountMapper.insert(companyAccount1);
                        break;
                    }
                    //如果能走到这一步，说明这就是个小公司
                    if (smallCompany != null){
                        //小公司提成
                        Double profit = project.getSmallCompanyProfit();
                        Double smallCompanyProfit = project.getMoney() * profit;
                        //1.记录分公司金额明细中，2.并且给该分公司发消息通知一下
                        //存入 个人流水账 表
                        PersonalMoney personalMoney = new PersonalMoney();
                        personalMoney.setTitle("开发客户提成");
                        personalMoney.setCreateTime(new Date());
                        personalMoney.setContent("开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle());
                        personalMoney.setType("0");//0 客户[分公司、合伙人]   1 员工
                        personalMoney.setPersonId(companyId);//个人id【流水账主人】
                        personalMoney.setMoney(smallCompanyProfit);
                        personalMoneyMapper.insert(personalMoney);

                        //通知分公司一声
                        Notice notice = new Notice();
                        notice.setContent("开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle()+" 提成: "+smallCompanyProfit);//通知消息内容
                        noticeMapper.insert(notice);
                        PersonalMessage personalMessage1 = new PersonalMessage();
                        personalMessage1.setMessageType("通知消息");
                        personalMessage1.setSenderId("系统");
                        personalMessage1.setSenderName("系统");
                        personalMessage1.setMessageLink(notice.getId());
                        personalMessage1.setCreateTime(new Date());
                        personalMessage1.setState("0");//消息状态 0未读
                        personalMessage1.setReceiverId(companyId);//分公司id
                        personalMessage1.setReceiverName(branchCompany.getName());

                        personalMessageMapper.insert(personalMessage1);

                        //给公司总账，添加一条明细记录
                        CompanyAccount companyAccount1 = new CompanyAccount();
                        companyAccount1.setTitle("分公司开发客户分成");            //公司明细标题
                        companyAccount1.setCreateTime(new Date());       //创建时间
                        companyAccount1.setMoney(-smallCompanyProfit);//支出金额
                        companyAccount1.setContent("分公司："+branchCompany.getName()+","+branchCompany.getPhone()+" 分得提成 "+ smallCompanyProfit);//记账内容
                        companyAccountMapper.insert(companyAccount1);

                        //如果这个小分公司【当前开发人】还有上级，再计算给上级的比例
                        if("1".equals(branchCompany.getType()) && branchCompany.getBranchCompanyId()!= null){
                            //这里的服务老师暂定为，小分公司的 负责人

//                            branchCompany.getBranchCompanyId();//上级公司 [客户身份]
                            Customer companySuper = customerMapper.selectById(branchCompany.getBranchCompanyId());
//                            branchCompany.getPrincipalCompanyId();//负责人 [员工身份]
                            Employee companyTeacher = employeeMapper.selectById(branchCompany.getPrincipalCompanyId());
                            //计算提成比例
                            Double gap = project.getBigCompanyProfit() - project.getSmallCompanyProfit();
                            Double superProfit = gap * branchProfitConfig.getCorporationElectCompanyElectVipBig();
                            Double teacherProfit = gap * branchProfitConfig.getCorporationElectCompanyElectVipTeacher();
                            //给上级大分公司记录提成
                            //存入 个人流水账 表
                            PersonalMoney personalMoneySuper = new PersonalMoney();
                            personalMoneySuper.setTitle("下级小分公司推荐了Vip");
                            personalMoneySuper.setCreateTime(new Date());
                            personalMoneySuper.setContent(branchCompany.getName()+","+branchCompany.getPhone()+" 开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle());
                            personalMoneySuper.setType("0");//0 客户[分公司、合伙人]   1 员工
                            personalMoneySuper.setPersonId(branchCompany.getBranchCompanyId());//个人id【流水账主人】
                            personalMoneySuper.setMoney(superProfit);//金额
                            personalMoneyMapper.insert(personalMoneySuper);
                            //给当前服务老师记录提成
                            //TODO 全部流水账改成这个模式
                            //存入 个人流水账 表
                            PersonalMoney personalMoneyTeacher = new PersonalMoney();
                            personalMoneyTeacher.setTitle("小分公司推荐了Vip");
                            personalMoneyTeacher.setCreateTime(new Date());
                            personalMoneyTeacher.setContent(branchCompany.getName()+","+branchCompany.getPhone()+" 开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle());
                            personalMoneyTeacher.setType("1");//0 客户[分公司、合伙人]   1 员工
                            personalMoneyTeacher.setPersonId(branchCompany.getPrincipalCompanyId());//个人id【流水账主人】
                            personalMoneyTeacher.setMoney(teacherProfit);//金额
                            personalMoneyMapper.insert(personalMoneyTeacher);
                            //公司总账减去 上面给出的提成
                            //给公司总账，添加一条明细记录
                            CompanyAccount companyAccountSuper = new CompanyAccount();
                            companyAccountSuper.setTitle("小分公司开发Vip，给上级提成");            //公司明细标题
                            companyAccountSuper.setCreateTime(new Date());       //创建时间
                            companyAccountSuper.setMoney(-superProfit);//支出金额
                            companyAccountSuper.setContent("上级分公司："+companySuper.getName()+","+companySuper.getPhone()+" 分得提成 "+ superProfit);//记账内容
                            companyAccountMapper.insert(companyAccountSuper);
                            //给公司总账，添加一条明细记录
                            CompanyAccount companyAccountTeacher = new CompanyAccount();
                            companyAccountTeacher.setTitle("小分公司开发Vip，给服务老师提成");            //公司明细标题
                            companyAccountTeacher.setCreateTime(new Date());       //创建时间
                            companyAccountTeacher.setMoney(-teacherProfit);//支出金额
                            companyAccountTeacher.setContent("服务老师："+companyTeacher.getName()+","+companyTeacher.getPhone()+" 分得提成 "+ teacherProfit);//记账内容
                            companyAccountMapper.insert(companyAccountTeacher);
                        }

                        break;
                    }

                    customer.setPrincipalVipId(customerRecord.getDivisionEmployeeId());
                    customerMapper.updateById(customer);
                    break;
                case "3": //小分公司
                    //如果大小分公司都有，优先大分公司身份
                    if (bigCompany != null){
                        //大公司直推小分公司提成
                        Double bigCompanyProfit = branchProfitConfig.getCorporationElectCompany();
                        //1.记录分公司金额明细中，2.并且给该分公司发消息通知一下
                        //存入 个人流水账 表
                        PersonalMoney personalMoney = new PersonalMoney();
                        personalMoney.setTitle("开发客户提成");
                        personalMoney.setCreateTime(new Date());
                        personalMoney.setContent("开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle());
                        personalMoney.setType("0");//0 客户[分公司、合伙人]   1 员工
                        personalMoney.setPersonId(companyId);//个人id【流水账主人】
                        personalMoney.setMoney(bigCompanyProfit);
                        personalMoneyMapper.insert(personalMoney);

                        //通知分公司一声
                        Notice notice = new Notice();
                        notice.setContent("开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle()+" 提成: "+bigCompanyProfit);//通知消息内容
                        noticeMapper.insert(notice);
                        PersonalMessage personalMessage1 = new PersonalMessage();
                        personalMessage1.setMessageType("通知消息");
                        personalMessage1.setSenderId("系统");
                        personalMessage1.setSenderName("系统");
                        personalMessage1.setMessageLink(notice.getId());
                        personalMessage1.setCreateTime(new Date());
                        personalMessage1.setState("0");//消息状态 0未读
                        personalMessage1.setReceiverId(companyId);//分公司id
                        personalMessage1.setReceiverName(branchCompany.getName());

                        personalMessageMapper.insert(personalMessage1);

                        //给公司总账，添加一条明细记录
                        CompanyAccount companyAccount1 = new CompanyAccount();
                        companyAccount1.setTitle("分公司开发客户分成");            //公司明细标题
                        companyAccount1.setCreateTime(new Date());       //创建时间
                        companyAccount1.setMoney(-bigCompanyProfit);//支出金额
                        companyAccount1.setContent("分公司："+branchCompany.getName()+","+branchCompany.getPhone()+" 分得提成 "+ bigCompanyProfit);//记账内容
                        companyAccountMapper.insert(companyAccount1);
                        break;
                    }
                    //小分公司
                    if (smallCompany != null){
                        //小公司直推小分公司提成
                        Double smallCompanyProfit =branchProfitConfig.getCompanyElectCompany();
                        //1.记录分公司金额明细中，2.并且给该分公司发消息通知一下
                        //存入 个人流水账 表
                        PersonalMoney personalMoney = new PersonalMoney();
                        personalMoney.setTitle("开发客户提成");
                        personalMoney.setCreateTime(new Date());
                        personalMoney.setContent("开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle());
                        personalMoney.setType("0");//0 客户[分公司、合伙人]   1 员工
                        personalMoney.setPersonId(companyId);//个人id【流水账主人】
                        personalMoney.setMoney(smallCompanyProfit);
                        personalMoneyMapper.insert(personalMoney);

                        //通知分公司一声
                        Notice notice = new Notice();
                        notice.setContent("开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle()+" 提成: "+smallCompanyProfit);//通知消息内容
                        noticeMapper.insert(notice);
                        PersonalMessage personalMessage1 = new PersonalMessage();
                        personalMessage1.setMessageType("通知消息");
                        personalMessage1.setSenderId("系统");
                        personalMessage1.setSenderName("系统");
                        personalMessage1.setMessageLink(notice.getId());
                        personalMessage1.setCreateTime(new Date());
                        personalMessage1.setState("0");//消息状态 0未读
                        personalMessage1.setReceiverId(companyId);//分公司id
                        personalMessage1.setReceiverName(branchCompany.getName());

                        personalMessageMapper.insert(personalMessage1);

                        //给公司总账，添加一条明细记录
                        CompanyAccount companyAccount1 = new CompanyAccount();
                        companyAccount1.setTitle("分公司开发客户分成");  //公司明细标题
                        companyAccount1.setCreateTime(new Date());    //创建时间
                        companyAccount1.setMoney(-smallCompanyProfit);//支出金额
                        companyAccount1.setContent("分公司："+branchCompany.getName()+","+branchCompany.getPhone()+" 分得提成 "+ smallCompanyProfit);//记账内容
                        companyAccountMapper.insert(companyAccount1);
                        break;
                    }

                    customer.setPrincipalCompanyId(customerRecord.getDivisionEmployeeId());
                    customerMapper.updateById(customer);
                    break;
                case "4": //大分公司
                    //大分公司
                    if (bigCompany != null){
                        //大公司直推大分公司提成
                        Double bigCompanyProfit = branchProfitConfig.getCorporationElectCorporation();
                        //1.记录分公司金额明细中，2.并且给该分公司发消息通知一下
                        //存入 个人流水账 表
                        PersonalMoney personalMoney = new PersonalMoney();
                        personalMoney.setTitle("开发客户提成");
                        personalMoney.setCreateTime(new Date());
                        personalMoney.setContent("开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle());
                        personalMoney.setType("0");//0 客户[分公司、合伙人]   1 员工
                        personalMoney.setPersonId(companyId);//个人id【流水账主人】
                        personalMoney.setMoney(bigCompanyProfit);
                        personalMoneyMapper.insert(personalMoney);

                        //通知分公司一声
                        Notice notice = new Notice();
                        notice.setContent("开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle()+" 提成: "+bigCompanyProfit);//通知消息内容
                        noticeMapper.insert(notice);
                        PersonalMessage personalMessage1 = new PersonalMessage();
                        personalMessage1.setMessageType("通知消息");
                        personalMessage1.setSenderId("系统");
                        personalMessage1.setSenderName("系统");
                        personalMessage1.setMessageLink(notice.getId());
                        personalMessage1.setCreateTime(new Date());
                        personalMessage1.setState("0");//消息状态 0未读
                        personalMessage1.setReceiverId(companyId);//分公司id
                        personalMessage1.setReceiverName(branchCompany.getName());

                        personalMessageMapper.insert(personalMessage1);

                        //给公司总账，添加一条明细记录
                        CompanyAccount companyAccount1 = new CompanyAccount();
                        companyAccount1.setTitle("分公司开发客户分成");            //公司明细标题
                        companyAccount1.setCreateTime(new Date());       //创建时间
                        companyAccount1.setMoney(-bigCompanyProfit);//支出金额
                        companyAccount1.setContent("分公司："+branchCompany.getName()+","+branchCompany.getPhone()+" 分得提成 "+ bigCompanyProfit);//记账内容
                        companyAccountMapper.insert(companyAccount1);
                        break;
                    }
                    //小分公司
                    if (smallCompany != null){
                        //小公司直推大分公司提成
                        Double smallCompanyProfit =branchProfitConfig.getCompanyElectCorporation();
                        //1.记录分公司金额明细中，2.并且给该分公司发消息通知一下
                        //存入 个人流水账 表
                        PersonalMoney personalMoney = new PersonalMoney();
                        personalMoney.setTitle("开发客户提成");
                        personalMoney.setCreateTime(new Date());
                        personalMoney.setContent("开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle());
                        personalMoney.setType("0");//0 客户[分公司、合伙人]   1 员工
                        personalMoney.setPersonId(companyId);//个人id【流水账主人】
                        personalMoney.setMoney(smallCompanyProfit);
                        personalMoneyMapper.insert(personalMoney);

                        //通知分公司一声
                        Notice notice = new Notice();
                        notice.setContent("开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle()+" 提成: "+smallCompanyProfit);//通知消息内容
                        noticeMapper.insert(notice);
                        PersonalMessage personalMessage1 = new PersonalMessage();
                        personalMessage1.setMessageType("通知消息");
                        personalMessage1.setSenderId("系统");
                        personalMessage1.setSenderName("系统");
                        personalMessage1.setMessageLink(notice.getId());
                        personalMessage1.setCreateTime(new Date());
                        personalMessage1.setState("0");//消息状态 0未读
                        personalMessage1.setReceiverId(companyId);//分公司id
                        personalMessage1.setReceiverName(branchCompany.getName());

                        personalMessageMapper.insert(personalMessage1);

                        //给公司总账，添加一条明细记录
                        CompanyAccount companyAccount1 = new CompanyAccount();
                        companyAccount1.setTitle("分公司开发客户分成");  //公司明细标题
                        companyAccount1.setCreateTime(new Date());    //创建时间
                        companyAccount1.setMoney(-smallCompanyProfit);//支出金额
                        companyAccount1.setContent("分公司："+branchCompany.getName()+","+branchCompany.getPhone()+" 分得提成 "+ smallCompanyProfit);//记账内容
                        companyAccountMapper.insert(companyAccount1);
                        break;
                    }

                    //如果是分公司开发，并且上级分公司id 不为空
                    if ("1".equals(customer.getType()) && StringUtils.isNotBlank(customer.getBranchCompanyId())){
                        //判断上级是 大分公司  还是  小分公司 计算对应提成
                        String superId = customer.getBranchCompanyId();//上级分公司id
                        Customer superCompany = customerMapper.selectById(superId);//上级公司对象
                        QueryWrapper<CustomerSmallCompany> smallQ = new QueryWrapper<>();
                        smallQ.eq("customer_id",superId);
                        CustomerSmallCompany small = customerSmallCompanyMapper.selectOne(smallQ);//小公司详情
                        QueryWrapper<CustomerBigCompany> bigQ = new QueryWrapper<>();
                        bigQ.eq("customer_id",superId);
                        CustomerBigCompany big = customerBigCompanyMapper.selectOne(bigQ);//大公司详情
                        //如果上级是大分公司
                        if (big != null){
                            Double bigProfit = branchProfitConfig.getCorporationElectCompanyUpgrade();

                            //存入【上级大分公司】 个人流水账 表
                            PersonalMoney personalMoney = new PersonalMoney();
                            personalMoney.setTitle("下级开发客户提成");
                            personalMoney.setCreateTime(new Date());
                            personalMoney.setContent("下级"+customerRecord.getDeveloper()+" 开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle());
                            personalMoney.setType("0");//0 客户[分公司、合伙人]   1 员工
                            personalMoney.setPersonId(superId);//个人id【流水账主人】
                            personalMoney.setMoney(bigProfit);
                            personalMoneyMapper.insert(personalMoney);

                            //通知上级分公司一声
                            Notice notice = new Notice();
                            notice.setContent("下级"+customerRecord.getDeveloper()+" 开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle()+" 提成: "+bigProfit);//通知消息内容
                            noticeMapper.insert(notice);
                            PersonalMessage personalMessage1 = new PersonalMessage();
                            personalMessage1.setMessageType("通知消息");
                            personalMessage1.setSenderId("系统");
                            personalMessage1.setSenderName("系统");
                            personalMessage1.setMessageLink(notice.getId());
                            personalMessage1.setCreateTime(new Date());
                            personalMessage1.setState("0");//消息状态 0未读
                            personalMessage1.setReceiverId(superId);//上级分公司id
                            personalMessage1.setReceiverName(superCompany.getName());

                            personalMessageMapper.insert(personalMessage1);

                            //给公司总账，添加一条明细记录
                            CompanyAccount companyAccount1 = new CompanyAccount();
                            companyAccount1.setTitle("分公司开发客户,上级分成");            //公司明细标题
                            companyAccount1.setCreateTime(new Date());       //创建时间
                            companyAccount1.setMoney(-bigProfit);//支出金额
                            companyAccount1.setContent("分公司："+superCompany.getName()+","+superCompany.getPhone()+" 分得提成 "+ bigProfit);//记账内容
                            companyAccountMapper.insert(companyAccount1);
                            break;
                        }
                        //如果上级是小分公司
                        if (small != null){
                            Double smallProfit = branchProfitConfig.getCompanyElectCompanyUpgrade();

                            //存入【上级小分公司】 个人流水账 表
                            PersonalMoney personalMoney = new PersonalMoney();
                            personalMoney.setTitle("下级开发客户提成");
                            personalMoney.setCreateTime(new Date());
                            personalMoney.setContent("下级"+customerRecord.getDeveloper()+" 开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle());
                            personalMoney.setType("0");//0 客户[分公司、合伙人]   1 员工
                            personalMoney.setPersonId(superId);//个人id【流水账主人】
                            personalMoney.setMoney(smallProfit);
                            personalMoneyMapper.insert(personalMoney);

                            //通知上级分公司一声
                            Notice notice = new Notice();
                            notice.setContent("下级"+customerRecord.getDeveloper()+" 开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle()+" 提成: "+smallProfit);//通知消息内容
                            noticeMapper.insert(notice);
                            PersonalMessage personalMessage1 = new PersonalMessage();
                            personalMessage1.setMessageType("通知消息");
                            personalMessage1.setSenderId("系统");
                            personalMessage1.setSenderName("系统");
                            personalMessage1.setMessageLink(notice.getId());
                            personalMessage1.setCreateTime(new Date());
                            personalMessage1.setState("0");//消息状态 0未读
                            personalMessage1.setReceiverId(superId);//上级分公司id
                            personalMessage1.setReceiverName(superCompany.getName());

                            personalMessageMapper.insert(personalMessage1);

                            //给公司总账，添加一条明细记录
                            CompanyAccount companyAccount1 = new CompanyAccount();
                            companyAccount1.setTitle("分公司开发客户,上级分成");            //公司明细标题
                            companyAccount1.setCreateTime(new Date());       //创建时间
                            companyAccount1.setMoney(-smallProfit);//支出金额
                            companyAccount1.setContent("分公司："+superCompany.getName()+","+superCompany.getPhone()+" 分得提成 "+ smallProfit);//记账内容
                            companyAccountMapper.insert(companyAccount1);
                            break;
                        }
                    }

                    customer.setPrincipalCompanyId(customerRecord.getDivisionEmployeeId());
                    customerMapper.updateById(customer);
                    break;
            }

            //③ 事业部员工,无论什么项目都提取3% 提成【这里不用计算，财务审核的时候，已经存了，直接拿来用】
                //获取后台设置的提成
//            CareerProfitConfig careerProfitConfig = careerProfitConfigMapper.selectById("1");
//            Double careerProfit = careerProfitConfig.getCareerProfit(); // 3%
              Double careerProfit = messageBuyVerify.getMoney();
              Employee customerCareer = employeeMapper.selectById(customerRecord.getDivisionEmployeeId());
            //存入【服务部员工】 个人流水账 表
            PersonalMoney personalMoney = new PersonalMoney();
            personalMoney.setTitle("为分公司录入客户提成");
            personalMoney.setCreateTime(new Date());
            personalMoney.setContent("分公司："+customerRecord.getDeveloper()+" 开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle());
            personalMoney.setType("1");//0 客户[分公司、合伙人]   1 员工
            personalMoney.setPersonId(customerRecord.getDivisionEmployeeId());//个人id【流水账主人】
            personalMoney.setMoney(careerProfit);
            personalMoneyMapper.insert(personalMoney);

            //给公司总账，添加一条明细记录
            CompanyAccount companyAccount1 = new CompanyAccount();
            companyAccount1.setTitle("事业部为分公司录入客户,提成");            //公司明细标题
            companyAccount1.setCreateTime(new Date());       //创建时间
            companyAccount1.setMoney(-careerProfit);//支出金额
            companyAccount1.setContent("分公司："+customerRecord.getDeveloper()+" 开发 "+customer.getName()+","+customer.getPhone()+" 购买 "+customerRecord.getProjectTitle()+" 事业部员工:"+customerCareer.getName()+" 提成 "+ careerProfit);//记账内容
            companyAccountMapper.insert(companyAccount1);


        }





        //找出当前购买项目的客户，经过上面的处理，这个客户肯定存在，如果不存在就说明，系统有问题
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", customerRecord.getPhone());
        Customer customer = customerMapper.selectOne(queryWrapper);
        if (customer == null){
            throw new LyException(ExceptionEnum.NO_NO_NO);
        }

        //2. 根据推荐人  计算给推荐人的提成,获取购买的项目的 对象
        //如果有推荐人
        if (StringUtils.isNotBlank(customerRecord.getReferralId())){
            QueryWrapper<CustomerVip> vipWrapper = new QueryWrapper<>();
            vipWrapper.eq("customer_id",customerRecord.getReferralId());
            CustomerVip customerVip = customerVipMapper.selectOne(vipWrapper);//获取推荐人的 vip详情
            //推荐人对象
            Customer customerReferral = customerMapper.selectById(customerRecord.getReferralId());
            switch (categoryId){
                case "1": //课程级别
                    Double referralCourseProfit = null;
                    if ("0".equals(customerVip.getState())){//vip没有提成

                    }else if ("1".equals(customerVip.getState())){//合伙人
                        referralCourseProfit = project.getMoney() * project.getPartnerProfit();

                        //存入【推荐人】 个人流水账 表
                        PersonalMoney personalMoney = new PersonalMoney();
                        personalMoney.setTitle("推荐提成");
                        personalMoney.setCreateTime(new Date());
                        personalMoney.setContent("推荐"+customerRecord.getDeveloper()+" 开发 "+customerRecord.getName()+","+customerRecord.getPhone()+" 购买 "+customerRecord.getProjectTitle());
                        personalMoney.setType("0");//0 客户[分公司、合伙人]   1 员工
                        personalMoney.setPersonId(customerRecord.getReferralId());//个人id【流水账主人】
                        personalMoney.setMoney(referralCourseProfit);
                        personalMoneyMapper.insert(personalMoney);


                        //给公司总账，添加一条明细记录
                        CompanyAccount companyAccount = new CompanyAccount();

                        companyAccount.setTitle("推荐提成"); //公司明细标题
                        companyAccount.setCreateTime(new Date()); //创建时间
                        companyAccount.setMoney(-referralCourseProfit);//支出金额
                        companyAccount.setContent("推荐人："+customerReferral.getName()+"  推荐"+customerRecord.getDeveloper()+" 开发 "+customerRecord.getName()+","+customerRecord.getPhone()+" 购买 "+customerRecord.getProjectTitle());//记账内容
                        companyAccountMapper.insert(companyAccount);
                    }
                    //vip或合伙人推荐做记录
                    CustomerVipRecord customerVipRecord0 = new CustomerVipRecord();
                    customerVipRecord0.setCreateTime(new Date());
                    customerVipRecord0.setCustomerId(customer.getId());
                    customerVipRecord0.setVipId(customerRecord.getReferralId());
                    customerVipRecord0.setProjectId(customerRecord.getProjectId());
                    customerVipRecord0.setProfit(referralCourseProfit);
                    customerVipRecordMapper.insert(customerVipRecord0);


                    break;
                case "2": //vip级别【目前规则：准客户推荐2个VIP、分公司 才能成为合伙人】
                    Double referralVipProfit = null;
                    //0 vip   1合伙人
                    if ("0".equals(customerVip.getState())){

                        //vip没有提成
                    }else if ("1".equals(customerVip.getState())){
                        //这里使用 添加项目 时候的提成比例
                        Double profitPercentage = project.getPartnerProfit();

                        referralVipProfit = project.getMoney() * profitPercentage;

                        //存入【推荐人】 个人流水账 表
                        PersonalMoney personalMoney = new PersonalMoney();
                        personalMoney.setTitle("推荐提成");
                        personalMoney.setCreateTime(new Date());
                        personalMoney.setContent("推荐"+customerRecord.getDeveloper()+" 开发 "+customerRecord.getName()+","+customerRecord.getPhone()+" 购买 "+customerRecord.getProjectTitle());
                        personalMoney.setType("0");//0 客户[分公司、合伙人]   1 员工
                        personalMoney.setPersonId(customerRecord.getReferralId());//个人id【流水账主人】
                        personalMoney.setMoney(referralVipProfit);
                        personalMoneyMapper.insert(personalMoney);

                        //给公司总账，添加一条明细记录
                        CompanyAccount companyAccount = new CompanyAccount();

                        companyAccount.setTitle("推荐提成"); //公司明细标题
                        companyAccount.setCreateTime(new Date()); //创建时间
                        companyAccount.setMoney(-referralVipProfit);//支出金额
                        companyAccount.setContent("推荐人："+customerReferral.getName()+"  推荐"+customerRecord.getDeveloper()+" 开发 "+customerRecord.getName()+","+customerRecord.getPhone()+" 购买 "+customerRecord.getProjectTitle());//记账内容
                        companyAccountMapper.insert(companyAccount);

                    }
                    //vip或合伙人推荐做记录
                    CustomerVipRecord customerVipRecord = new CustomerVipRecord();
                    customerVipRecord.setCreateTime(new Date());
                    customerVipRecord.setCustomerId(customer.getId());
                    customerVipRecord.setVipId(customerRecord.getReferralId());
                    customerVipRecord.setProjectId(customerRecord.getProjectId());
                    customerVipRecord.setProfit(referralVipProfit);
                    customerVipRecordMapper.insert(customerVipRecord);
                    //更新身份
                    customerVip.setCount(customerVip.getCount() + 1);//增加vip推荐次数
                    if (customerVip.getCount() > 2){
                        customerVip.setState("1");
                    }
                    customerVipMapper.updateById(customerVip);//更新记录[vip与合伙人身份]
                    break;
                case "3"://小分公司
                    Double referralSmallProfit = null;
                    //0 vip   1合伙人
                    if ("0".equals(customerVip.getState())){

                        //vip没有提成
                    }else if ("1".equals(customerVip.getState())){
                        //这里使用 添加项目 时候的提成比例

                        //查看上级是什么？
                        String superId = customerReferral.getBranchCompanyId();
                        if (StringUtils.isBlank(superId)){//仁仁家直属下级合伙人

                        }else{
                            //分别查找上级  大分公司详情，小分公司详情
                            QueryWrapper<CustomerSmallCompany> smallQuery = new QueryWrapper<>();
                            smallQuery.eq("customer_id",superId);
                            CustomerSmallCompany smallCompany = customerSmallCompanyMapper.selectOne(smallQuery);//小公司详情
                            QueryWrapper<CustomerBigCompany> bigQuery = new QueryWrapper<>();
                            bigQuery.eq("customer_id",superId);
                            CustomerBigCompany bigCompany =customerBigCompanyMapper.selectOne(bigQuery);//大公司详情
                            if (smallCompany != null){//小分公司
                                //小分公司合伙人直推小分公司
                                referralSmallProfit = partnerProfitConfig.getCompanyPartnerElectCompany();
                            }
                            if (bigCompany != null){//大分公司
                                //大分公司合伙人直推小分公司
                                referralSmallProfit = partnerProfitConfig.getCorporationPartnerElectCompany();
                            }

                        }

                        //存入【推荐人】 个人流水账 表
                        PersonalMoney personalMoney = new PersonalMoney();
                        personalMoney.setTitle("推荐提成");
                        personalMoney.setCreateTime(new Date());
                        personalMoney.setContent("推荐"+customerRecord.getDeveloper()+" 开发 "+customerRecord.getName()+","+customerRecord.getPhone()+" 购买 "+customerRecord.getProjectTitle());
                        personalMoney.setType("0");//0 客户[分公司、合伙人]   1 员工
                        personalMoney.setPersonId(customerRecord.getReferralId());//个人id【流水账主人】
                        personalMoney.setMoney(referralSmallProfit);
                        personalMoneyMapper.insert(personalMoney);

                        //给公司总账，添加一条明细记录
                        CompanyAccount companyAccount = new CompanyAccount();

                        companyAccount.setTitle("推荐提成"); //公司明细标题
                        companyAccount.setCreateTime(new Date()); //创建时间
                        companyAccount.setMoney(-referralSmallProfit);//支出金额
                        companyAccount.setContent("推荐人："+customerRecord.getDeveloper()+"  推荐"+customerRecord.getDeveloper()+" 开发 "+customerRecord.getName()+","+customerRecord.getPhone()+" 购买 "+customerRecord.getProjectTitle());//记账内容
                        companyAccountMapper.insert(companyAccount);

                    }
                    //vip或合伙人推荐做记录
                    CustomerVipRecord customerVipRecord1 = new CustomerVipRecord();
                    customerVipRecord1.setCreateTime(new Date());
                    customerVipRecord1.setCustomerId(customer.getId());
                    customerVipRecord1.setVipId(customerRecord.getReferralId());
                    customerVipRecord1.setProjectId(customerRecord.getProjectId());
                    customerVipRecord1.setProfit(referralSmallProfit);
                    customerVipRecordMapper.insert(customerVipRecord1);
                    //更新身份
                    customerVip.setCount(customerVip.getCount() + 1);//增加vip推荐次数
                    if (customerVip.getCount() > 2){
                        customerVip.setState("1");
                    }
                    customerVipMapper.updateById(customerVip);//更新记录[vip与合伙人身份]

                    break;
                case "4"://大分公司
                    Double referralBigProfit = 0.0;
                    //0 vip   1合伙人
                    if ("0".equals(customerVip.getState())){

                        //vip没有提成
                    }else if ("1".equals(customerVip.getState())){
                        //这里使用 添加项目 时候的提成比例

                        //查看上级是什么？
                        String superId = customerReferral.getBranchCompanyId();
                        if (StringUtils.isBlank(superId)){//仁仁家直属下级合伙人

                        }else{
                            //分别查找上级  大分公司详情，小分公司详情
                            QueryWrapper<CustomerSmallCompany> smallQuery = new QueryWrapper<>();
                            smallQuery.eq("customer_id",superId);
                            CustomerSmallCompany smallCompany = customerSmallCompanyMapper.selectOne(smallQuery);//小公司详情
                            QueryWrapper<CustomerBigCompany> bigQuery = new QueryWrapper<>();
                            bigQuery.eq("customer_id",superId);
                            CustomerBigCompany bigCompany =customerBigCompanyMapper.selectOne(bigQuery);//大公司详情
                            if (smallCompany != null){//小分公司
                                //小分公司合伙人直推大分公司
                                referralBigProfit = partnerProfitConfig.getCompanyPartnerElectCorporation();
                            }
                            if (bigCompany != null){//大分公司
                                //大分公司合伙人直推大分公司
                                referralBigProfit = partnerProfitConfig.getCorporationPartnerElectCorporation();
                            }

                        }

                        //存入【推荐人】 个人流水账 表
                        PersonalMoney personalMoney = new PersonalMoney();
                        personalMoney.setTitle("推荐提成");
                        personalMoney.setCreateTime(new Date());
                        personalMoney.setContent("推荐"+customerRecord.getDeveloper()+" 开发 "+customerRecord.getName()+","+customerRecord.getPhone()+" 购买 "+customerRecord.getProjectTitle());
                        personalMoney.setType("0");//0 客户[分公司、合伙人]   1 员工
                        personalMoney.setPersonId(customerRecord.getReferralId());//个人id【流水账主人】
                        personalMoney.setMoney(referralBigProfit);
                        personalMoneyMapper.insert(personalMoney);

                        //给公司总账，添加一条明细记录
                        CompanyAccount companyAccount = new CompanyAccount();

                        companyAccount.setTitle("推荐提成"); //公司明细标题
                        companyAccount.setCreateTime(new Date()); //创建时间
                        companyAccount.setMoney(-referralBigProfit);//支出金额
                        companyAccount.setContent("推荐人："+customerRecord.getDeveloper()+"  推荐"+customerRecord.getDeveloper()+" 开发 "+customerRecord.getName()+","+customerRecord.getPhone()+" 购买 "+customerRecord.getProjectTitle());//记账内容
                        companyAccountMapper.insert(companyAccount);

                    }
                    //vip或合伙人推荐做记录
                    CustomerVipRecord customerVipRecord2 = new CustomerVipRecord();
                    customerVipRecord2.setCreateTime(new Date());
                    customerVipRecord2.setCustomerId(customer.getId());
                    customerVipRecord2.setVipId(customerRecord.getReferralId());
                    customerVipRecord2.setProjectId(customerRecord.getProjectId());
                    customerVipRecord2.setProfit(referralBigProfit);
                    customerVipRecordMapper.insert(customerVipRecord2);
                    //更新身份
                    customerVip.setCount(customerVip.getCount() + 1);//增加vip推荐次数
                    if (customerVip.getCount() > 2){
                        customerVip.setState("1");
                    }
                    customerVipMapper.updateById(customerVip);//更新记录[vip与合伙人身份]

                    break;
            }
        }



        //3.插入课程时间，到客户拥有的课程表，项目所属的分类id 1课程  2vip
        //如果是课程和vip则进行如下操作
        if (categoryId.equals("1") || categoryId.equals("2")){


            //查询购买的项目的包含的课程的id数组
            String courseList = projectMapper.selectById(customerRecord.getProjectId()).getCourse();
            List<String> stringList = JsonUtils.parseList(courseList, String.class);//课程id 集合
            List<ProjectCourse> projectCourses = projectCourseMapper.selectBatchIds(stringList);
            for (ProjectCourse projectCours : projectCourses) {
                String projectCoursId = projectCours.getId();//根据后台课程的id，查询对应的上课时间
                List<CourseTime> courseTimes = courseTimeMapper.queryListByCourseId(projectCoursId);

                CustomerCourseClass customerCourseClass = new CustomerCourseClass();
                customerCourseClass.setProjectId(customerRecord.getProjectId());//该课程属于的项目
                customerCourseClass.setCourseId(projectCours.getId());
                customerCourseClass.setCourseTitle(projectCours.getTitle());
                customerCourseClass.setCustomerId(customer.getId());//设置客户id
                customerCourseClassMapper.insert(customerCourseClass);//插入客户所拥有的课程表
                //给用户课时表，插入对应该课程的课时
                String customerCourseClassId = customerCourseClass.getId();//刚刚插入用户拥有课程表返回的id
                for (CourseTime courseTime : courseTimes) {
                    CustomerCourseClassTime customerCourseClassTime = new CustomerCourseClassTime();
                    customerCourseClassTime.setCourseClassId(customerCourseClassId);
                    customerCourseClassTime.setStartTime(courseTime.getStartTime());
                    customerCourseClassTime.setEndTime(courseTime.getEndTime());
                    customerCourseClassTime.setState("0");
                    customerCourseClassTimeMapper.insert(customerCourseClassTime);
                }
            }
        }

        //4.为分公司级别的分配赠送的课程款或VIP名额   3 小分公司   4 大分公司
        if (categoryId.equals("3") || categoryId.equals("4")){
            //小分公司，分配赠送课程
            if (categoryId.equals("3")){

                //为小分公司分配 课程名额
                Integer companyCourseNumber = branchProfitConfig.getCompanyCourseNumber();//赠送数量
                String companyCourseProjectId = branchProfitConfig.getProjectId();//赠送课程的id
                CustomerSmallCompany customerSmallCompany = new CustomerSmallCompany();
                customerSmallCompany.setCount(companyCourseNumber);
                customerSmallCompany.setCustomerId(customer.getId());
                customerSmallCompany.setCustomerName(customer.getName());
                customerSmallCompany.setProjectId(companyCourseProjectId);//赠送的项目的id
                customerSmallCompanyMapper.insert(customerSmallCompany);//插入 小分公司详情

            }
            //大分公司，分配赠送课程款
            if (categoryId.equals("4")){

                //为大分公司分配 课程款
                Double corporationMoney = branchProfitConfig.getCorporationMoney();//赠送课程款
                CustomerBigCompany customerBigCompany = new CustomerBigCompany();
                customerBigCompany.setCustomerId(customer.getId());
                customerBigCompany.setCustomerName(customer.getName());
                customerBigCompany.setCourseAccount(corporationMoney);//课程款
                this.customerBigCompanyMapper.insert(customerBigCompany);//在大分公司 详情中 插入

            }
        }


        //5.给公司总账，添加一条明细记录
            CompanyAccount companyAccount = new CompanyAccount();
            companyAccount.setTitle("开发客户");            //公司明细标题
            companyAccount.setCreateTime(new Date());       //创建时间
            companyAccount.setMoney(customerRecord.getMoney());//收入金额
            companyAccount.setContent(customerRecord.getName()+" 购买 "+customerRecord.getProjectTitle());//记账内容
            companyAccountMapper.insert(companyAccount);



        //6.如果该客户曾经是潜在客户，把潜在客户表的状态state改成1
        potentialCustomerService.transfer(customer.getPhone());
    }


}
